js时间戳转换几分前几小时前几天前几周前
Posted 奥特曼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js时间戳转换几分前几小时前几天前几周前相关的知识,希望对你有一定的参考价值。
function caculateTimeago(dateTimeStamp)
const minute = 1000 * 60; // 把分,时,天,周,半个月,一个月用毫秒表示
const hour = minute * 60;
const day = hour * 24;
const week = day * 7;
const now = new Date().getTime(); // 获取当前时间毫秒
const diffValue = now - dateTimeStamp; // 时间差
let result = '';
if (diffValue < 0)
return;
const minC = diffValue / minute; // 计算时间差的分,时,天,周,月
const hourC = diffValue / hour;
const dayC = diffValue / day;
const weekC = diffValue / week;
if (weekC >= 1 && weekC <= 4)
result = ` $parseInt(weekC, 10)周前`;
else if (dayC >= 1 && dayC <= 6)
result = ` $parseInt(dayC, 10)天前`;
else if (hourC >= 1 && hourC <= 23)
result = ` $parseInt(hourC, 10)小时前`;
else if (minC >= 1 && minC <= 59)
result = ` $parseInt(minC, 10)分钟前`;
else if (diffValue >= 0 && diffValue <= minute)
result = '刚刚';
else
const datetime = new Date();
datetime.setTime(dateTimeStamp);
const Nyear = datetime.getFullYear();
const Nmonth = datetime.getMonth() + 1 < 10 ? `0$datetime.getMonth() + 1` : datetime.getMonth() + 1;
const Ndate = datetime.getDate() < 10 ? `0$datetime.getDate()` : datetime.getDate();
result = `$Nyear-$Nmonth-$Ndate`;
return result;
以上是关于js时间戳转换几分前几小时前几天前几周前的主要内容,如果未能解决你的问题,请参考以下文章
js 显示友好的时间格式刚刚几分钟前几小时几天前几周前几月前等等 时间格式化(工具类)
js 实现几分钟前几小时前几天前,以及几分钟后几小时后几天前后