DateDiff

时间计算,返回刚刚,xxx分钟前,xxx小时前,xxx天前等

Detta skript bör inte installeras direkt. Det är ett bibliotek för andra skript att inkludera med meta-direktivet // @require https://update.greasyfork.org/scripts/412357/853742/DateDiff.js

  1. // ==UserScript==
  2. // @name DateDiff
  3. // @namespace http://bbs.91wc.net/
  4. // @version 0.1.0
  5. // @description 时间计算,返回刚刚,xxx分钟前,xxx小时前,xxx天前等
  6. // @author wishking
  7. // ==/UserScript==
  8.  
  9. ;(function(window){
  10. /**
  11. * [dateDiff 算时间差]
  12. * @param {[type=Number]} hisTime [历史时间戳,必传]
  13. * @param {[type=Number]} nowTime [当前时间戳,不传将获取当前时间戳]
  14. * @return {[string]} [string]
  15. */
  16. var dateDiff = function(hisTime,nowTime){
  17. var now =nowTime?nowTime:new Date().getTime(),
  18. diffValue = now - hisTime,
  19. result='',
  20. minute = 1000 * 60,
  21. hour = minute * 60,
  22. day = hour * 24,
  23. halfamonth = day * 15,
  24. month = day * 30,
  25. year = month * 12,
  26. _year = diffValue/year,
  27. _month =diffValue/month,
  28. _week =diffValue/(7*day),
  29. _day =diffValue/day,
  30. _hour =diffValue/hour,
  31. _min =diffValue/minute;
  32. if(_year>=1) result=parseInt(_year) + "年前";
  33. else if(_month>=1) result=parseInt(_month) + "个月前";
  34. else if(_week>=1) result=parseInt(_week) + "周前";
  35. else if(_day>=1) result=parseInt(_day) +"天前";
  36. else if(_hour>=1) result=parseInt(_hour) +"个小时前";
  37. else if(_min>=1) result=parseInt(_min) +"分钟前";
  38. else result="刚刚";
  39. return result;
  40. }
  41. window.dateDiff = dateDiff
  42. })(window);