formatTime.js 4.8 KB

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