You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

date.ts 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import dayjs from 'dayjs';
  2. /**
  3. * 计算两个日期之间经过的时间,如 "3分12秒"
  4. *
  5. * @param {string | null | undefined} begin - The starting date.
  6. * @param {string | null | undefined} end - The ending date.
  7. * @return {string} The formatted elapsed time string.
  8. */
  9. export const elapsedTime = (begin?: string | Date | null, end?: string | Date | null): string => {
  10. if (begin === undefined || begin === null) {
  11. return '--';
  12. }
  13. if (end === undefined || end === null) {
  14. return '--';
  15. }
  16. const beginDate = dayjs(begin);
  17. const endDate = dayjs(end); // end === undefined || end === null ? dayjs(now()) : dayjs(end);
  18. if (!beginDate.isValid() || !endDate.isValid()) {
  19. return '--';
  20. }
  21. const timestamp = endDate.valueOf() - beginDate.valueOf();
  22. if (timestamp < 0) {
  23. return '0秒';
  24. }
  25. const duration = dayjs.duration(timestamp);
  26. const years = duration.years();
  27. const months = duration.months();
  28. const days = duration.days();
  29. const hours = duration.hours();
  30. const minutes = duration.minutes();
  31. const seconds = duration.seconds();
  32. const elspsedArray = [];
  33. if (years !== 0) {
  34. elspsedArray.push(`${years}年`);
  35. }
  36. if (months !== 0) {
  37. elspsedArray.push(`${months}个月`);
  38. }
  39. if (days !== 0) {
  40. elspsedArray.push(`${days}天`);
  41. }
  42. if (hours !== 0) {
  43. elspsedArray.push(`${hours}小时`);
  44. }
  45. if (minutes !== 0) {
  46. elspsedArray.push(`${minutes}分`);
  47. }
  48. if (seconds !== 0) {
  49. elspsedArray.push(`${seconds}秒`);
  50. }
  51. if (elspsedArray.length === 0) {
  52. return '0秒';
  53. }
  54. return elspsedArray.slice(0, 2).join('');
  55. };
  56. /**
  57. * 是否是有效的日期
  58. *
  59. * @param {Date} date - The date to check.
  60. * @return {boolean} True if the date is valid, false otherwise.
  61. */
  62. export const isValidDate = (date: Date): boolean => {
  63. if (date instanceof Date) {
  64. return !Number.isNaN(date.getTime()); // valueOf() 也可以
  65. }
  66. return false;
  67. };
  68. /**
  69. * 能否使用 dayjs 转换成 Date
  70. *
  71. * @param {Date | string | number | null | undefined} date - The date to be checked.
  72. * @return {boolean} Returns true if the date can be converted to a valid Date object, otherwise returns false.
  73. */
  74. export const canBeConvertToDate = (date?: Date | string | number | null): boolean => {
  75. if (date === undefined || date === null || date === '') {
  76. return false;
  77. }
  78. const convertedDate = dayjs(date);
  79. return convertedDate.isValid();
  80. };
  81. /**
  82. * 转换日期
  83. *
  84. * @param {Date | string | number | null | undefined} date - The date to format.
  85. * @param {string} [format='YYYY-MM-DD HH:mm:ss'] - The format string to use.
  86. * @return {string} The formatted date string.
  87. */
  88. export const formatDate = (
  89. date: Date | string | number | null | undefined,
  90. format: string = 'YYYY-MM-DD HH:mm:ss',
  91. ): string => {
  92. if (date === undefined || date === null || date === '') {
  93. return '--';
  94. }
  95. const convertedDate = dayjs(date);
  96. if (!convertedDate.isValid()) {
  97. return '--';
  98. }
  99. return convertedDate.format(format);
  100. };