大家好!我叫戴向天。
QQ:809002582
前端開發常用方法 - formatTime - 時間戳格式轉換以及計算
代碼如下👇
/**
* 時間戳格式轉換以及計算
* */
export function formatTime (time = 0, format = 'YYYY-MM-DD hh:mm:ss') {
const now = new Date().getTime()
if (!time) time = now
while (time.toString().length < 13) time += '0'
const date = new Date(time)
date.getMonth()
/** 參數集 年-月-日 時:分:秒 */
const arg = {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds()
}
/** 判斷有沒有指定的時間格式 */
switch (format) {
case 'YYYY-MM-DD hh:mm:ss':
return `${arg.year}-${arg.month}-${arg.day} ${arg.hours}:${arg.minutes}:${arg.seconds}`
case 'YYYY-MM-DD':
return `${arg.year}-${arg.month}-${arg.day}`
case 'MM-DD':
return `${arg.month}-${arg.day}`
case 'hh:mm:ss':
return `${arg.hours}:${arg.minutes}:${arg.seconds}`
case 'hh:mm':
return `${arg.hours}:${arg.minutes}`
case 'computed': //判斷是不是需要進行計算
if (now > time) {
const dt = Math.abs(time - now), //時間已過去多少毫秒
S = dt / 1000, //秒
M = S / 60, //分
H = M / 60, //小時
D = H / 24, //天
W = D / 7 //周
/**
~~ ==>表示取整數部分 類似與 parseInt
*/
if (~~W > 0 && W < 3) {
return ~~W + '周前'
} else if (D < 7 && ~~D > 0) {
return ~~D + '天前'
} else if (~~H > 0 && H < 24) {
return ~~H + '小時前'
} else if (~~M > 0 && M < 60) {
return ~~M + '分鐘前'
} else if (~~S > 0 && S < 60) {
return ~~S + '秒前'
}
} else {
console.log('未來的時間')
}
return `${arg.year}-${arg.month}-${arg.day} ${arg.hours}:${arg.minutes}:${arg.seconds}`
}
}