一、
1、时间格式化
this.time是时分,例如12:23
let subTimeFormate = this.format(this.time,2)
2、计算星期
//值从0到6
let nowWeek = nowDate.getDay();
let arr=['周日','周一','周二','周三','周四','周五','周六']
let week_one=arr[nowWeek]
return week_one //返回的是周几
二、几个小时的时间戳转化为时分秒
//data为时间戳,data的单位为秒
function time1(data){
var h = Math.floor(data/60/60);
var m = Math.floor(data/60%60);
var s = Math.floor(data%60);
return h + '时' + m + '分' + s + '秒'
}
三、通过时间戳来算出发布时间
time是十位数
var timeone =time*1000
//当发布的时间距离当前时间超过24小时,就显示年月日
if(new Date().getTime()-timeone>86400000){
const times = new Date(timeone);
const Y = times.getFullYear();
console.log(Y)
let M = times.getMonth() + 1;
M = M < 10 ? '0' + M : M;
let D = times.getDate();
D = D < 10 ? '0' + D : D;
return `${Y}年${M}月${D}日`
}
//当发布的时间距离当前时间不超过24小时,就显示秒分小时
else{
const duration = (+new Date().getTime() - timeone) / 1000;
let displayTime = ''
if (duration < 60) {
console.log('秒')
displayTime = `${Math.round(Math.max(duration, 1))} 秒前`;
}else if (duration < 60*60) {
console.log('分')
displayTime = `${Math.round(duration / 60)} 分钟前`;
}else if (duration < 60*60*24) {
console.log('小时')
displayTime = `${Math.round(duration / 60 / 60)} 小时前`;
}else {
displayTime = dateTime(recordTime);
}
return displayTime
}