阅读:3076回复:0
js 格式化当前时间戳 vue
data.js:
export function timestampToTime(timestamp) { let date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000 let Y = date.getFullYear() + '-'; let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'; let D = (date.getDate()+1 < 10 ? '0'+(date.getDate()+1) : date.getDate()) + ' '; let h = (date.getHours()+1 < 10 ? '0'+(date.getHours()+1) : date.getHours()) + ':'; let m = (date.getMinutes()+1 < 10 ? '0'+(date.getMinutes()+1) : date.getMinutes()) + ':'; let s = date.getSeconds()+1 < 10 ? '0'+(date.getSeconds()+1) : date.getSeconds(); return Y+M+D+h+m+s; } vue: import {timestampToTime} from "@/utils/dateTime.js" const t1 = new Date().valueOf() // 第一种,推荐 const t2 = new Date().getTime() // 第二种,推荐 let time = timestampToTime(t2) |
|