Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 给一个时间戳,返回距离现在多久了
- * @param time 一个时间戳
- * @returns {*} 时间差
- */
- function returnTime(time) {
- if (isNaN(time) === true)
- return "Error!" + '"' + time + '"' + " is NaN";
- if (time < 0) return "Error! 过去的时间大于现在的时间!";
- else {
- var diffValue = (Date.now() - time) / 1000 / 60;
- if (diffValue < 1) return "刚刚";
- if (diffValue < 60) return parseInt(diffValue) + "分钟前";
- if (diffValue < 60 * 24) return parseInt(diffValue / 60) + "小时前";
- if (diffValue < 60 * 24 * 30) return parseInt(diffValue / 60 / 24) + "天前";
- return parseInt(diffValue / 60 / 24 / 30) + "月前";
- }
- }
- document.write(returnTime(1509019200000));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement