Home  >  Article  >  Web Front-end  >  How to convert milliseconds to date in vuejs

How to convert milliseconds to date in vuejs

藏色散人
藏色散人Original
2021-09-24 11:34:153129browse

vuejs将毫秒转成日期的方法:1、定义过滤器“filters.js”;2、在所用之处引入“filters.js”;3、通过“formatDate(data,'yyyy-MM-dd');”进行转换即可。

How to convert milliseconds to date in vuejs

本文操作环境:Windows7系统、vue2.9.6版,DELL G3电脑。

vuejs怎么将毫秒转成日期?

vue将毫秒数转化为正常日期格式的实例

过滤器-时间戳转化

第一步:定义过滤器,filters.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);
 };

第二步:在所用之处引入filters.js

import {formatDate} from '../../common/filters.js'

第三步:使用

filters:{
 formatDate(time){
  var data = new Date(time);
  return formatDate(data,'yyyy-MM-dd');
 }
 },

<span>{{item.rateTime | formatDate}}</span>

相关推荐:《vue.js教程

The above is the detailed content of How to convert milliseconds to date in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn