(1)创建一个处理时间格式的js,内容如下:

../../utils/formatDate.js

export function formatDate(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + ''
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
}
}
return fmt
} function padLeftZero(str) {
return ('00' + str).substr(str.length)
}

(2)在vue文件中需要格式化时间戳的地方,使用filters过滤器,做如下处理:

<template>
<div class="date">{{item.pass_time | formatDate}}</div>
</template>

<script type="text/ecmascript-6">
 // 引入formatDate.js 文件
import {formatDate} from '../../utils/formatDate.js'
export default {
filters: {
    
    //方法一: yyyy-MM-dd hh:mm
formatDate(time) {
time = time * 1000
let date = new Date(time)
console.log(new Date(time))
return formatDate(date, 'yyyy-MM-dd hh:mm')
}     
     //方法二: yyyy-MM-dd
formatDate(time) {
// time = time * 1000
let date = new Date(time)
console.log(new Date(time))
return formatDate(date, 'yyyy-MM-dd')
}
} }

</script>

  补充:time应为格式为13位unix时间戳,如果拿到的时间戳是10位的unix时间戳,因此需要乘以1000。

转载地址: https://blog.csdn.net/qq_32678401/article/details/81983364

vue 将时间戳转换成日期格式 (一)的更多相关文章

  1. js angular 时间戳转换成日期格式 年月日 yyyy-MM-dd

    昨天写项目,要把时间戳转换成日期格式发给后端 我就去网上找 看到的一些都不是我想要的 索性自己就写了一个如图 下面是angular 模式 $scope.getMyDate = function(str ...

  2. js将时间戳转换成日期格式-陈远波

    var timestamp =1539598555000;//时间戳 //时间戳转换成time格式function timestampToTime(timestamp) { var date = ne ...

  3. jqgrid 时间戳转换成日期格式

    原文 :http://blog.csdn.net/caoyuancsdn/article/details/52984524 Java script  接收到的时间参数是时间戳*1000 functio ...

  4. javaScript中将时间戳转换成日期格式

    function DateFormt(time, format) { ); var o = { , "d+": testDate.getDate(), "h+" ...

  5. js时间戳转成日期格式

    将时间戳转换成日期格式:// 简单的一句代码var date = new Date(时间戳); //获取一个时间对象 注意:如果是uinx时间戳记得乘于1000.比如php函数time()获得的时间戳 ...

  6. javascript时间戳转换成指定格式的日期

    //时间戳转换成指定格式的日期DateTool.IntDatetimeTo = function(time, format){    var testDate = new Date(time);    ...

  7. moment使用,把某个时间时间戳转换成日期

    1.某个时间时间戳转换成日期 moment(时间戳 ).format("YYYYMMDD")   2.获取某个日期当月的最后一天 moment(“2019-04-05”).endO ...

  8. CTime,Systemtime的比较还有转换成日期格式。

    vc为我们提供了两种日期型的变量. 一种是CTime.他的缺点就是年份只支持到2038年,以后的日期就不支持啦,如果你的项目有20-30年的寿命,你就选择使用SYSTEMTIME.这个时间函数来进行比 ...

  9. vue filters 时间戳转化成时间格式

    vue filters 时间戳转化成时间格式 filters: { formatDate: function (time) { var re = /-?\d+/ var m = re.exec(tim ...

随机推荐

  1. Python3基础 变量命名 区分大小写

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  2. OpenBLAS编译 Debug x64 Win10 vs2015

    OpenBLAS编译  Debug x64  Win10  vs2015 >------ 已启动生成: 项目: ZERO_CHECK, 配置: Debug x64 ------ > Che ...

  3. Python利用ctypes实现按引用传参

    C的代码 void test_cref(char *a, int *b, char *data) { , sizeof(char)); strcpy(p, "cute"); a[] ...

  4. 条件概率和链式法则 conditional probability & chain rule

    顾名思义, 条件概率指的是某个事件在给定其他条件时发生的概率, 这个非常符合人的认知:我们通常就是在已知一定的信息(条件)情况下, 去估计某个事件可能发生的概率. 概率论中,用 | 表示条件, 条件概 ...

  5. ASP.NET全球化与本地化 c#多国语言的支持 (项目支持多国语言的开发)

    ASP.NET 2.0及以上的开发平台,为全球化本地化应用程序提供了工具,而且实现起来非常简单.以下内容是使用c#,按照帮助一步步做的,将为初学者提供详细的实现步骤. 一 几个必要概念 (一) 支持全 ...

  6. 【Leetcode_easy】953. Verifying an Alien Dictionary

    problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...

  7. 【VS开发】windows下的signal

    在windows下,信号机制简单来说是通过工作线程实现的,该线程运行于相对优先级THREAD_PRIORITY_HIGHEST,当信号产生时,windows生成该线程执行信号处理逻辑,由于该线程优先级 ...

  8. 修正线性单元(Rectified linear unit,ReLU)

    修正线性单元(Rectified linear unit,ReLU) Rectified linear unit 在神经网络中,常用到的激活函数有sigmoid函数f(x)=11+exp(−x).双曲 ...

  9. 在eNSP上简单的模拟企业网络场景(不同网段互连)

    额..首先你要有eNSP工具和Wireshark抓包工具,没有的话可以上网搜索一下,最好下载最新版本的,新版本中拥有更多型号的机器 这个实验我们主要模拟某公司购买了新的路由器和交换机.交换机S1连接客 ...

  10. VMware中安装Ubntu

    "懦夫没有能力去表现爱:爱是勇者的特权"----甘地 原文请见: https://blog.csdn.net/wumumang/article/details/54099997 一 ...