vue 时间戳转今天,昨天,以前时间
filters:{
formatDate(date) {
const lastDate = new Date(date);
lastDate.setHours(0);
lastDate.setMinutes(0);
lastDate.setSeconds(0);
const mistiming = Math.round((Date.now() - lastDate.getTime()) / 1000)
const tags = ['年', '个月', '星期', '天', '小时', '分钟', '秒']
const times = [31536000, 2592000, 604800, 86400, 3600, 60, 1]
for (let i = 0; i < times.length; i++) {
const inm = Math.floor(mistiming / times[i])
if (tags[i] === '天') {
switch (inm) {
case 0:
return '今天'
break
case 1:
return '昨天'
break
default:
// return inm + tags[i] + '前'
timestampToTime(date)
break;
}
}
if (inm !== 0) {
return timestampToTime(date)
}
}
function timestampToTime(timestamp) {
let date =new Date(timestamp),//时间戳为10位需*1000,时间戳为13位的话不需乘1000
Y = date.getFullYear(),
M = (date.getMonth() +1 <10 ?'0' + (date.getMonth() +1) : date.getMonth() +1),
D = date.getDate(),
h = date.getHours() +':',
m = date.getMinutes() +':',
s = date.getSeconds();
return Y +'-'+M +'-'+D; //时分秒可以根据自己的需求加上
}
},
},
以下是具体的月星期
function formatDate(date) {
const mistiming = Math.round((Date.now() - new Date(date).getTime()) / 1000)
const tags = ['年', '个月', '星期', '天', '小时', '分钟', '秒']
const times = [31536000, 2592000, 604800, 86400, 3600, 60, 1]
for (let i = 0; i < times.length; i++) {
const inm = Math.floor(mistiming / times[i])
if (tags[i] === '天') {
switch (inm) {
case 0:
return '今天'
break
case 1:
return '昨天'
break
case 2:
return '前天'
break
default:
return inm + tags[i] + '前'
break;
}
}
if (inm !== 0) {
return inm + tags[i] + '前'
}
}
}
除今年日期之前的都添加年
function timestampToTime(timestamp) {
let date =new Date(timestamp),//时间戳为10位需*1000,时间戳为13位的话不需乘1000
Y = date.getFullYear(),
M = (date.getMonth() +1 <10 ?'0' + (date.getMonth() +1) : date.getMonth() +1),
D = date.getDate(),
h = date.getHours() +':',
m = date.getMinutes() +':',
s = date.getSeconds(),
newY=new Date().getFullYear(); //2021
// console.log(newY>=Y)
// console.log(newY,'今年时间======') //2020
// console.log(Y,'去年时间======') //2020
if(newY===Y){ //
return M +'-'+D; //时分秒可以根据自己的需求加上
}else{
return Y +'-'+M +'-'+D;
}
}
版权声明:本文由Web学习之路发布,如需转载请注明出处。