1.正常時間格式:2020-10-10 08:56:28
2.時間格式轉(zhuǎn)時間戳:new Date('2020-10-10 08:56:28').getTime()
3.時間戳轉(zhuǎn)時間格式:
dateTime(now) {
var date = new Date(now * 1000); //時間戳為10位需*1000,時間戳為13位的話不需乘1000
var Y = date.getFullYear() + '-';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
return Y + M + D + h + m + s;
}
//在需要用到的時候調(diào)用,傳參是需要轉(zhuǎn)換的時間戳
4.秒轉(zhuǎn)換成時間
formatSeconds(value) {
var theTime = parseInt(value);// 秒
var middle= 0;// 分
var hour= 0;// 小時
if(theTime > 60) {
middle= parseInt(theTime/60);
theTime = parseInt(theTime%60);
if(middle> 60) {
hour= parseInt(middle/60);
middle= parseInt(middle%60);
}
}
var result = ""+parseInt(theTime)+"秒";
if(middle > 0) {
result = ""+parseInt(middle)+"分"+result;
}
if(hour> 0) {
result = ""+parseInt(hour)+"小時"+result;
}
return result;
}
5.秒數(shù)轉(zhuǎn)換倒計時:00:08:25
// 時間轉(zhuǎn)換
formatBit(val) {
val = +val;
return val > 9 ? val : "0" + val;
}, // 秒轉(zhuǎn)時分秒,求模很重要,數(shù)字的下舍入
formatSeconds(time) {
let min = Math.floor(time % 3600);
let val =
this.formatBit(Math.floor(time / 3600)) +
":" +
this.formatBit(Math.floor(min / 60)) +
":" +
this.formatBit(time % 60);
return val;
}, // 定時器
minReturn(time_limit) {
//參數(shù)為秒*60,參數(shù)為分鐘不需要
let time = time_limit * 60;
let t = setInterval(() => {
time--;
//定義全局變量 timeDown 作為在頁面上展示
//下面一行業(yè)可以更改為 return this.formatSeconds(time) 沒嘗試過
this.timeDown = this.formatSeconds(time);
if (time <= 0) {
clearInterval(t);
}
}, 1000);
},
6.時間戳轉(zhuǎn)換為剛剛、XX分鐘前、XX小時前、XX天前等等等
放在export default{}的外面然后在組件中通過過濾方式來進行調(diào)用
export function timeago(dateTimeStamp) {
// dateTimeStamp是一個時間毫秒,注意時間戳是秒的形式,在這個毫秒的基礎(chǔ)上除以1000,就是十位數(shù)的時間戳。13位數(shù)的都是時間毫秒。
let minute = 1000 * 60; //把分,時,天,周,半個月,一個月用毫秒表示
let hour = minute * 60;
let day = hour * 24;
let week = day * 7;
//var halfamonth = day * 15;
let month = day * 30;
let now = new Date().getTime(); //獲取當(dāng)前時間毫秒
let diffValue = now - dateTimeStamp;//時間差
if (diffValue < 0) { return; }
let minC = diffValue / minute; //計算時間差的分,時,天,周,月
let hourC = diffValue / hour;
let dayC = diffValue / day;
let weekC = diffValue / week;
let monthC = diffValue / month;
let result
if (monthC >= 1) {
result = "" + parseInt(monthC) + "月前";
}
else if (weekC >= 1) {
result = "" + parseInt(weekC) + "周前";
}
else if (dayC >= 1) {
result = "" + parseInt(dayC) + "天前";
}
else if (hourC >= 1) {
result = "" + parseInt(hourC) + "小時前";
}
else if (minC >= 1) {
result = "" + parseInt(minC) + "分鐘前";
} else
result = "剛剛";
return result;
}
放在export default{}的里面
filters:{
//調(diào)用 Timer 方法
Timer: function(str) {
//時間戳為10位數(shù)的需要*1000
let times = Number(str) * 1000;
return timeago(times);
}
},
在頁面上使用
<span>{{ item.create_time | Timer(item.create_time) }}</span>