formatTime.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * 时间日期转换
  3. * @param date 当前时间,new Date() 格式
  4. * @param format 需要转换的时间格式字符串
  5. * @description format 字符串随意,如 `YYYY-mm、YYYY-mm-dd`
  6. * @description format 季度:"YYYY-mm-dd HH:MM:SS QQQQ"
  7. * @description format 星期:"YYYY-mm-dd HH:MM:SS WWW"
  8. * @description format 几周:"YYYY-mm-dd HH:MM:SS ZZZ"
  9. * @description format 季度 + 星期 + 几周:"YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ"
  10. * @returns 返回拼接后的时间字符串
  11. */
  12. export function formatDate(date: Date, format: string): string {
  13. let we = date.getDay(); // 星期
  14. let z = getWeek(date); // 周
  15. let qut = Math.floor((date.getMonth() + 3) / 3).toString(); // 季度
  16. const opt: { [key: string]: string } = {
  17. 'Y+': date.getFullYear().toString(), // 年
  18. 'm+': (date.getMonth() + 1).toString(), // 月(月份从0开始,要+1)
  19. 'd+': date.getDate().toString(), // 日
  20. 'H+': date.getHours().toString(), // 时
  21. 'M+': date.getMinutes().toString(), // 分
  22. 'S+': date.getSeconds().toString(), // 秒
  23. 'q+': qut, // 季度
  24. };
  25. // 中文数字 (星期)
  26. const week: { [key: string]: string } = {
  27. '0': '日',
  28. '1': '一',
  29. '2': '二',
  30. '3': '三',
  31. '4': '四',
  32. '5': '五',
  33. '6': '六',
  34. };
  35. // 中文数字(季度)
  36. const quarter: { [key: string]: string } = {
  37. '1': '一',
  38. '2': '二',
  39. '3': '三',
  40. '4': '四',
  41. };
  42. if (/(W+)/.test(format))
  43. format = format.replace(RegExp.$1, RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' + week[we] : '周' + week[we]) : week[we]);
  44. if (/(Q+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 4 ? '第' + quarter[qut] + '季度' : quarter[qut]);
  45. if (/(Z+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 3 ? '第' + z + '周' : z + '');
  46. for (let k in opt) {
  47. let r = new RegExp('(' + k + ')').exec(format);
  48. // 若输入的长度不为1,则前面补零
  49. if (r) format = format.replace(r[1], RegExp.$1.length == 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, '0'));
  50. }
  51. return format;
  52. }
  53. /**
  54. * 获取当前日期是第几周
  55. * @param dateTime 当前传入的日期值
  56. * @returns 返回第几周数字值
  57. */
  58. export function getWeek(dateTime: Date): number {
  59. let temptTime = new Date(dateTime.getTime());
  60. // 周几
  61. let weekday = temptTime.getDay() || 7;
  62. // 周1+5天=周六
  63. temptTime.setDate(temptTime.getDate() - weekday + 1 + 5);
  64. let firstDay = new Date(temptTime.getFullYear(), 0, 1);
  65. let dayOfWeek = firstDay.getDay();
  66. let spendDay = 1;
  67. if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1;
  68. firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay);
  69. let d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000);
  70. let result = Math.ceil(d / 7);
  71. return result;
  72. }
  73. /**
  74. * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
  75. * @param param 当前时间,new Date() 格式或者字符串时间格式
  76. * @param format 需要转换的时间格式字符串
  77. * @description param 10秒: 10 * 1000
  78. * @description param 1分: 60 * 1000
  79. * @description param 1小时: 60 * 60 * 1000
  80. * @description param 24小时:60 * 60 * 24 * 1000
  81. * @description param 3天: 60 * 60* 24 * 1000 * 3
  82. * @returns 返回拼接后的时间字符串
  83. */
  84. export function formatPast(param: string | Date, format: string = 'YYYY-mm-dd'): string {
  85. // 传入格式处理、存储转换值
  86. let t: any, s: number;
  87. // 获取js 时间戳
  88. let time: number = new Date().getTime();
  89. // 是否是对象
  90. typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param);
  91. // 当前时间戳 - 传入时间戳
  92. time = Number.parseInt(`${time - t}`);
  93. if (time < 10000) {
  94. // 10秒内
  95. return '刚刚';
  96. } else if (time < 60000 && time >= 10000) {
  97. // 超过10秒少于1分钟内
  98. s = Math.floor(time / 1000);
  99. return `${s}秒前`;
  100. } else if (time < 3600000 && time >= 60000) {
  101. // 超过1分钟少于1小时
  102. s = Math.floor(time / 60000);
  103. return `${s}分钟前`;
  104. } else if (time < 86400000 && time >= 3600000) {
  105. // 超过1小时少于24小时
  106. s = Math.floor(time / 3600000);
  107. return `${s}小时前`;
  108. } else if (time < 259200000 && time >= 86400000) {
  109. // 超过1天少于3天内
  110. s = Math.floor(time / 86400000);
  111. return `${s}天前`;
  112. } else {
  113. // 超过3天
  114. let date = typeof param === 'string' || 'object' ? new Date(param) : param;
  115. return formatDate(date, format);
  116. }
  117. }
  118. /**
  119. * 时间问候语
  120. * @param param 当前时间,new Date() 格式
  121. * @description param 调用 `formatAxis(new Date())` 输出 `上午好`
  122. * @returns 返回拼接后的时间字符串
  123. */
  124. export function formatAxis(param: Date): string {
  125. let hour: number = new Date(param).getHours();
  126. if (hour < 6) return '凌晨好';
  127. else if (hour < 9) return '早上好';
  128. else if (hour < 12) return '上午好';
  129. else if (hour < 14) return '中午好';
  130. else if (hour < 17) return '下午好';
  131. else if (hour < 19) return '傍晚好';
  132. else if (hour < 22) return '晚上好';
  133. else return '夜里好';
  134. }