Home > Web Front-end > Vue.js > body text

How to convert milliseconds to date in vuejs

藏色散人
Release: 2023-01-13 00:45:39
Original
3138 people have browsed it

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);
 };
Copy after login

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

import {formatDate} from '../../common/filters.js'
Copy after login

第三步:使用

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

<span>{{item.rateTime | formatDate}}</span>
Copy after login

相关推荐:《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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template