Vue–时间戳转换日期格式

Vue–时间戳转换日期格式vue 时间戳转换日期格式后台返回的时间戳格式 例如 creatTime 90 需要用时间格式显示 需要 2021 07 21 格式就用这种写法 templateslot scope scope spanv if scope row creatTime null parseTime scope row creatTime y m spanv if scope row creatTime null templateslot scope scope

vue时间戳转换日期格式

一,vue获取时间戳转换为日期格式

后台返回的时间戳格式(例如:creatTime: 90),需要用时间格式显示 

(1)需要2021-09-28格式显示

在这里插入图片描述

 <el-table-column align="center" label="通知时间"> <template slot-scope="scope"> <span v-if="scope.row.creatTime != null"> { 
   { 
    parseTime(scope.row.creatTime, "{y}-{m}-{d}") }} </span> </template> </el-table-column> 

(2)需要2021-09-28 10:13:09格式显示

在这里插入图片描述

 <el-table-column align="center" label="通知时间"> <template slot-scope="scope"> <span v-if="scope.row.creatTimes!= null"> { 
   { 
    parseTime(scope.row.creatTime ) }} </span> </template> </el-table-column> 

二, 需要向后台传时间戳格式的写法 如下格式

(1)2020-09-28格式转时间戳

在这里插入图片描述

 return{ 
    startTime:"", endTime:"", } 
 startTime:new Date(this.form.startTime).getTime() endTime: new Date(this.form.endTime).getTime() 

(2)如果开始时间或者结束时间取当天时间

 return{ 
    startTime: new Date(), endTime:"", } 
 startTime: new Date(this.form.startTime).getTime() endTime: new Date(this.form.endTime).getTime() 

(3)如下格式 2021-09-28—2021-09-30格式

在这里插入图片描述

 <el-form-item> <span class="demonstration">日期筛选:</span> <el-date-picker v-model="createTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" > </el-date-picker> </el-form-item> 
 return{ 
    createTime:"", } 
 startTime:this.createTime && this.createTime[0] ? new Date(this.createTime[0]).getTime() : "", endTime:this.createTime && this.createTime[1] ? new Date(this.createTime[1]).getTime(): "", 

三,获取当前的年月日时分秒并展示

 
  
{ { nowtime }}
 return{ 
    nowtime:"" } mounted(){ 
    setInterval(() => { 
    this.getTime(); }, 1000); }, methods:{ 
    getTime() { 
    this.nowtime = parseTime(new Date(), '{y}年{m}月{d}日 {h}:{i}:{s} 周{a}'); }, } 

在这里插入图片描述

四,需要传(2021-12-16)

<el-date-picker type="date" placeholder="选择日期" v-model="auditorPostponeTime"> </el-date-picker> 
 let times = ''; if (this.auditorPostponeTime) { 
    times = parseTime(this.auditorPostponeTime, '{y}-{m}-{d}'); } let req={ 
    auditorPostponeTime: times, //同意选择的时间 } 

五,注意:代码中必须要引入date.js文件,并在方法中使用即可,否则以上不成立

 import { 
    parseTime } from "@/utils/date"; 

(1)创建一个date.js文件,内容如下:

/ * Parse the time to string * @param {(Object|string|number)} time * @param {string} cFormat * @returns {string | null} */ export function parseTime(time, cFormat) { if (arguments.length === 0) { return null } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time = parseInt(time) } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { const value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] } return value.toString().padStart(2, '0') }) return time_str } / * @param {number} time * @param {string} option * @returns {string} */ export function formatTime(time, option) { if (('' + time).length === 10) { time = parseInt(time) * 1000 } else { time = +time } const d = new Date(time) const now = Date.now() const diff = (now - d) / 1000 if (diff < 30) { return '刚刚' } else if (diff < 3600) { // less 1 hour return Math.ceil(diff / 60) + '分钟前' } else if (diff < 3600 * 24) { return Math.ceil(diff / 3600) + '小时前' } else if (diff < 3600 * 24 * 2) { return '1天前' } if (option) { return parseTime(time, option) } else { return ( d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分' ) } } / * @param {string} url * @returns {Object} */ export function param2Obj(url) { const search = url.split('?')[1] if (!search) { return {} } return JSON.parse( '{"' + decodeURIComponent(search) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') .replace(/\+/g, ' ') + '"}' ) } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/178402.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午5:23
下一篇 2026年3月26日 下午5:23


相关推荐

  • 前端vue定时器_vue切换页面清除所有定时器

    前端vue定时器_vue切换页面清除所有定时器vue页面定时器的两种方式

    2025年7月29日
    4
  • I bumped into a girl literally_back and forth

    I bumped into a girl literally_back and forthhttp://acm.hznu.edu.cn/OJ/problem.php?cid=1263&amp;pid=6http://acm.hznu.edu.cn/OJ/problem.php?id=2585题意:每天能往存钱罐加任意实数的钱,每天不能多于起那一天放的钱数。如果某一天的钱数恰好等于那天的特价商品,则可以买,求最后的最大快乐值。题解:显然的贪心:如果第i天买完,准备在第…

    2025年8月12日
    5
  • Java集合面试题[通俗易懂]

    Java集合面试题Java集合框架的基础接口有哪些?Collection,为集合层级的根接口。一个集合代表一组对象,这些对象即为它的元素。Java平台不提供这个接口任何直接的实现。Set,是一个不能包含重复元素的集合。这个接口对数学集合抽象进行建模,被用来代表集合,就如一副牌。List,是一个有序集合,可以包含重复元素。你可以通过它的索引来访问任何元素。List更像长度动态…

    2022年4月7日
    63
  • 红黑树与平衡二叉树_百图详解红黑树

    红黑树与平衡二叉树_百图详解红黑树之前在公司组内分享了红黑树的工作原理 今天把它整理下发出来 希望能对大家有所帮助 对自己也算是一个知识点的总结 这篇文章算是我写博客写公众号以来画图最多的一篇文章了 没有之一 我希望尽可能多地用图片来形象地描述红黑树的各种操作的前后变换原理 帮助大家来理解红黑树的工作原理 下面 多图预警开始了 在讲红黑树之前 我们首先来了解下下面几个概念 二叉树 排序二叉树以及平衡二叉树 二叉树二叉树指的是每个节点最多只能有两个字数的有序树 通常左边的子树称为左子树 右边的子树称为右子树 这里说的有序树

    2026年3月26日
    2
  • java有什么作用_Java有什么用「建议收藏」

    java有什么作用_Java有什么用「建议收藏」1.可以62616964757a686964616fe58685e5aeb931333431353261做WEB系统,如网站,不过做网站就有点杀鸡用牛刀了。用的最多的就是用JAVA做管理系统,很容易维护。2。可以做C/S非WEB管理系统,当然其他语言也能做到。3。可以做移动设备软件,如手机软件,MP4软件等等。4。当然你WINDOWS下做好了一个东西,拿到其他地方也能用,如LINUX,NUIX等等…

    2022年7月8日
    20
  • 程序员私活网汇总

    程序员私活网汇总1 IT 外包 http itwaibao com 2 CODING 码市 https mart coding net3 开源众包 https zb oschina net projects list html4 猪八戒 https zbj com5 程序员客栈 https www proginn com

    2026年3月19日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号