Vue中将时间戳转换为年月日
用法
时间戳转换为年月日 yyyy-MM-dd hh:mm
用法1: formatDate(new Date(val * 1000), ‘yyyy年MM月dd日 hh:mm’) val为10位数时间戳
用法2: formatDate(date, ‘yyyy年MM月dd日 hh:mm’) date为 Tue Oct 25 09:44:28 GMT+0800 (GMT+08:00)
1657715711147
这是时间戳
let date = new Date(时间戳)let time = formatDate(date, 'yyyy-MM-dd hh:mm')//time = -06-01 00:00
把对应时间戳的变量填进去,
一定是13位时间戳, 如果不是 就乘1000 要把毫秒数算上
方法
写在vue的methods里面,如果不是在vue里面用的话
就把里面的 this 删除
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 : this.padLeftZero(str))}}return fmt},padLeftZero(str) {return ('00' + str).substr(str.length)}
这是方法,接下来看怎么调用
let date = new Date(时间戳)
formatDate(date, ‘yyyy-MM-dd hh:mm’)
这是执行方法👆
date为 Fri Apr 01 00:00:00 GMT+0800 (中国标准时间)
【Vue】时间戳转换为年月日 格式为 yyyy-MM-dd hh:mm js转换时间戳 时间戳转换为年月日 Fri Apr 01 00:00:00 GMT+0800 (中国标准时间)转换