datetime.js 1.5 KB

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