如何将时间戳转换为普通日期格式,封装在utlis文件中请看下面代码:
将封装的时间戳函数单独放在一个js文件中
// 时间戳,年-月-日 时-分-秒export function formatDate(now) {var year = now.getFullYear(); //取得4位数的年份var month = addzoer(now.getMonth() + 1); //取得日期中的月份,其中0表示1月,11表示12月var date = addzoer(now.getDate()); //返回日期月份中的天数(1到31)var hour = addzoer(now.getHours()); //返回日期中的小时数(0到23)var minute = addzoer(now.getMinutes()); //返回日期中的分钟数(0到59)var second = addzoer(now.getSeconds()); //返回日期中的秒数(0到59)return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;//定义函数判断时间小于十前面加0function addzoer(timE) {if (timE < 10) {return '0' + timE} else {return timE}}}
页面的使用方法,将封装的时间戳函数进行调用
//调用封装的时间戳函数 formatDate<template>{{`获取的时间戳` | showDate}}</template><script>import { formatDate } from "封装的路径";//在filters中转化已有的时间戳filters: {showDate(value) {// 1.将时间戳转为Date对象const date = new Date(value * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000// 2.将date进行格式化return formatDate(date, "yyyy-MM-dd hh:mm:ss");}}</script>