Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. /**
  2. * 给一个时间戳,返回距离现在多久了
  3. * @param time 一个时间戳
  4. * @returns {*} 时间差
  5. */
  6. function returnTime(time) {
  7. if (isNaN(time) === true)
  8. return "Error!" + '"' + time + '"' + " is NaN";
  9. if (time < 0) return "Error! 过去的时间大于现在的时间!";
  10. else {
  11. var diffValue = (Date.now() - time) / 1000 / 60;
  12. if (diffValue < 1) return "刚刚";
  13. if (diffValue < 60) return parseInt(diffValue) + "分钟前";
  14. if (diffValue < 60 * 24) return parseInt(diffValue / 60) + "小时前";
  15. if (diffValue < 60 * 24 * 30) return parseInt(diffValue / 60 / 24) + "天前";
  16. return parseInt(diffValue / 60 / 24 / 30) + "月前";
  17. }
  18. }
  19.  
  20. document.write(returnTime(1509019200000));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement