datetime.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function relativeDate(dtstr) {
  4. if (!dtstr)
  5. return '暂无数据';
  6. const dt = new Date(dtstr);
  7. const dateTimeStamp = dt.getTime();
  8. const second = 1000;
  9. const minute = second * 60;
  10. const hour = minute * 60;
  11. const day = hour * 24;
  12. const month = day * 30;
  13. const now = new Date().getTime();
  14. const diffValue = now - dateTimeStamp;
  15. if (diffValue < 0) {
  16. return;
  17. }
  18. const monthC = diffValue / month;
  19. const weekC = diffValue / (7 * day);
  20. const dayC = diffValue / day;
  21. const hourC = diffValue / hour;
  22. const minC = diffValue / minute;
  23. const secC = diffValue / second;
  24. let result;
  25. if (monthC > 12) {
  26. const y = dt.getFullYear() + ' 年';
  27. const m = dt.getMonth() + 1 + ' 月';
  28. const d = dt.getDate() + ' 日';
  29. result = [y, m, d].join(' ');
  30. }
  31. else if (monthC >= 1) {
  32. result = '' + Math.floor(monthC) + ' 个月前';
  33. }
  34. else if (weekC >= 1) {
  35. result = '' + Math.floor(weekC) + ' 周前';
  36. }
  37. else if (dayC >= 1) {
  38. result = '' + Math.floor(dayC) + ' 天前';
  39. }
  40. else if (hourC >= 1) {
  41. result = '' + Math.floor(hourC) + ' 小时前';
  42. }
  43. else if (minC >= 1) {
  44. result = '' + Math.floor(minC) + ' 分钟前';
  45. }
  46. else
  47. result = '' + Math.floor(secC) + ' 秒前';
  48. return result;
  49. }
  50. exports.relativeDate = relativeDate;