util.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. export const formatTime = date => {
  2. const year = date.getFullYear();
  3. const month = date.getMonth() + 1;
  4. const day = date.getDate();
  5. const hour = date.getHours();
  6. const minute = date.getMinutes();
  7. const second = date.getSeconds();
  8. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(
  9. ':');
  10. };
  11. const formatNumber = n => {
  12. n = n.toString();
  13. return n[1] ? n : '0' + n;
  14. };
  15. export const getNowdate = date => {
  16. const year = date.getFullYear();
  17. const month = date.getMonth() + 1;
  18. const day = date.getDate();
  19. const hour = date.getHours();
  20. const minute = date.getMinutes();
  21. const second = date.getSeconds();
  22. return year + '-' + month + '-' + day;
  23. };
  24. /**
  25. * 日期格式化
  26. * @param { String | Objcet } time js 时间格式的字符串; java 时间格式的字符串; js 时间对象
  27. * @param { String } pattern day.js 的占位符字符
  28. * @returns String 格式化后的时间
  29. */
  30. export const parseTime = (time, pattern) => {
  31. if (arguments.length === 0 || !time) {
  32. return null
  33. }
  34. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  35. let date
  36. if (typeof time === 'object') {
  37. date = time
  38. } else {
  39. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  40. time = parseInt(time)
  41. } else if (typeof time === 'string') {
  42. time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
  43. }
  44. if ((typeof time === 'number') && (time.toString().length === 10)) {
  45. time = time * 1000
  46. }
  47. date = new Date(time)
  48. }
  49. const formatObj = {
  50. y: date.getFullYear(),
  51. m: date.getMonth() + 1,
  52. d: date.getDate(),
  53. h: date.getHours(),
  54. i: date.getMinutes(),
  55. s: date.getSeconds(),
  56. a: date.getDay()
  57. }
  58. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  59. let value = formatObj[key]
  60. // Note: getDay() returns 0 on Sunday
  61. if (key === 'a') {
  62. return ['日', '一', '二', '三', '四', '五', '六'][value]
  63. }
  64. if (result.length > 0 && value < 10) {
  65. value = '0' + value
  66. }
  67. return value || 0
  68. })
  69. return time_str
  70. }
  71. /**
  72. * @desc 格式化日期字符串
  73. * @param { Nubmer} - 时间戳
  74. * @returns { String } 格式化后的日期字符串
  75. */
  76. export function formatDate(timestamp) {
  77. // 补全为13位
  78. let arrTimestamp = (timestamp + '').split('');
  79. for (let start = 0; start < 13; start++) {
  80. if (!arrTimestamp[start]) {
  81. arrTimestamp[start] = '0';
  82. }
  83. }
  84. timestamp = arrTimestamp.join('') * 1;
  85. let minute = 1000 * 60;
  86. let hour = minute * 60;
  87. let day = hour * 24;
  88. let month = day * 30;
  89. let now = new Date().getTime();
  90. let diffValue = now - timestamp;
  91. // 如果本地时间反而小于变量时间
  92. if (diffValue < 0) {
  93. return '不久前';
  94. }
  95. // 计算差异时间的量级
  96. let monthC = diffValue / month;
  97. let weekC = diffValue / (7 * day);
  98. let dayC = diffValue / day;
  99. let hourC = diffValue / hour;
  100. let minC = diffValue / minute;
  101. // 数值补0方法
  102. let zero = function (value) {
  103. if (value < 10) {
  104. return '0' + value;
  105. }
  106. return value;
  107. };
  108. if (monthC > 4) {
  109. // 超过1年,直接显示年月日
  110. return (function () {
  111. let date = new Date(timestamp);
  112. return date.getFullYear() + '-' + zero(date.getMonth() + 1) + '-' + zero(date.getDate()) + '-';
  113. })();
  114. } else if (monthC >= 1) {
  115. return parseInt(monthC) + '月前';
  116. } else if (weekC >= 1) {
  117. return parseInt(weekC) + '周前';
  118. } else if (dayC >= 1) {
  119. return parseInt(dayC) + '天前';
  120. } else if (hourC >= 1) {
  121. return parseInt(hourC) + '小时前';
  122. } else if (minC >= 1) {
  123. return parseInt(minC) + '分钟前';
  124. }
  125. return '刚刚';
  126. }