在vue中的src目录下新建utils文件夹 ,在其下面新建date.js文件,写一个时间转换的函数
4c4f253ffa7908fbf029012325238f4.png
代码如下
export function formatTime (date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
然后在其需要转换时间的页面中引用它
import { formatTime } from "@/utils/date.js";
然后调用其函数
577343a4369dfad3d94997116828cf1.png
注释(this.value1[0]为后台数据获取的时间)
let date = formatTime(date)
或
const articlesDisplay = articles.map(data => {
data.created_time_display = formatTime(data.created_time);
return data
})