tools.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import dayjs from 'dayjs';
  2. export function formatSeconds(seconds, type) {
  3. let hours = Math.floor(seconds / 3600);
  4. let minutes = Math.floor((seconds - (hours * 3600)) / 60);
  5. let secs = seconds - (hours * 3600 + minutes * 60);
  6. hours = hours == 0 ? '' : hours.toString().padStart(2, '0');
  7. minutes = hours == 0 && minutes == 0 ? '' : minutes.toString().padStart(2, '0');
  8. secs = hours == 0 && minutes == 0&&secs == 0 ? '' : secs.toString().padStart(2, '0');
  9. // 汉字1,:隔开0
  10. if(type == 1) {
  11. return `${hours&&hours+'时'}${minutes&&minutes+'分'}${secs&&secs+'秒'}`
  12. } else {
  13. return `${hours&&hours+':'}${minutes&&minutes+':'}${secs}`
  14. }
  15. };
  16. //新增
  17. var friendlyDate=function(timestamp) {
  18. var formats = {
  19. 'year': '%n% 年前',
  20. 'month': '%n% 月前',
  21. 'day': '%n% 天前',
  22. 'hour': '%n% 小时前',
  23. 'minute': '%n% 分钟前',
  24. 'second': '%n% 秒前',
  25. };
  26. var now = Date.now();
  27. var seconds = Math.floor((now - timestamp) / 1000);
  28. var minutes = Math.floor(seconds / 60);
  29. var hours = Math.floor(minutes / 60);
  30. var days = Math.floor(hours / 24);
  31. var months = Math.floor(days / 30);
  32. var years = Math.floor(months / 12);
  33. var diffType = '';
  34. var diffValue = 0;
  35. if (years > 0) {
  36. diffType = 'year';
  37. diffValue = years;
  38. } else {
  39. if (months > 0) {
  40. diffType = 'month';
  41. diffValue = months;
  42. } else {
  43. if (days > 0) {
  44. diffType = 'day';
  45. diffValue = days;
  46. } else {
  47. if (hours > 0) {
  48. diffType = 'hour';
  49. diffValue = hours;
  50. } else {
  51. if (minutes > 0) {
  52. diffType = 'minute';
  53. diffValue = minutes;
  54. } else {
  55. diffType = 'second';
  56. diffValue = seconds === 0 ? (seconds = 1) : seconds;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. return formats[diffType].replace('%n%', diffValue);
  63. }
  64. // /**
  65. // * 格式化时间
  66. // * @param {Object} 时间字符串
  67. // */
  68. export function formatDate(dateStr) {
  69. let formatStr = "";
  70. let formatDate=null;
  71. if(dateStr==null){
  72. return formatStr;
  73. }
  74. let date = dayjs(dateStr,'YYYY-MM-DD HH:mm:ss');
  75. let today = dayjs();
  76. if (date.year() != today.year()) {
  77. formatDate = date.format('YYYY-MM-DD HH:mm');
  78. }
  79. else if (date.month() === today.month()) {
  80. switch (date.date() - today.date()) {
  81. case 0:
  82. formatDate = date.format('今天 HH:mm');
  83. break;
  84. case -1:
  85. formatDate = date.format('昨天 HH:mm');
  86. break;
  87. case 1:
  88. formatDate = date.format('明天 HH:mm');
  89. break;
  90. default:
  91. formatDate = date.format('MM-DD HH:mm');
  92. break;
  93. }
  94. }
  95. else if (date.month() - today.month() === 0 && date.date() === 1) {
  96. formatDate = date.format('明天 HH:mm');
  97. }
  98. else {
  99. formatDate = date.format('MM-DD HH:mm');
  100. }
  101. return formatDate;
  102. }
  103. // /**
  104. // * 格式化时间
  105. // * @param {Object} 时间字符串
  106. // */
  107. var formatHour=function(dateStr) {
  108. let date = dayjs(dateStr,'YYYY-MM-DD HH:mm:ss');
  109. let formatHour = date.format('HH:mm');
  110. return formatHour;
  111. }
  112. // /**
  113. // * 计算时间差值
  114. // * @param {startTime} 开始时间
  115. // * @param {endTime} 结束时间
  116. // * @param {type} 类型
  117. // */
  118. var calculateDiffTime=function(startTime, endTime,type=4) {
  119. // let timeStarts = startTime.getTime(); //开始时间,转换成时间戳
  120. // let timeEnds = endTime.getTime(); //结束时间,转换成时间戳
  121. let timer = endTime - startTime //将时间戳进行相减
  122. let year = parseInt((timer / 1000 / 60 / 60 / 24 / 30 / 12));
  123. let month = parseInt((timer / 1000 / 60 / 60 / 24 / 30));
  124. let day = parseInt((timer / 1000 / 60 / 60 / 24));
  125. let hour = parseInt((timer / 1000 / 60 / 60 % 24));
  126. let minute = parseInt((timer / 1000 / 60 % 60));
  127. let second = parseInt((timer / 1000 % 60));
  128. if(hour>0){
  129. return (hour>10?hour:'0'+hour) + ':' + (minute>10?minute:'0'+minute) + ':' + (second>10?second:'0'+second);
  130. }else if(minute>0){
  131. return (minute>=10?minute:'0'+minute) + ':' + (second>=10?second:'0'+second);
  132. }else if(second>0){
  133. return '00:'+(second>=10?second:'0'+second);
  134. }
  135. // if (type === 1) { // 返回相差年数
  136. // return year + '年'
  137. // } else if (type === 2) { // 返回相差年数月数
  138. // return year + '年' + month + '月'
  139. // } else if (type === 3) { // 返回相差年数月数天数
  140. // return year + '年' + month + '月' + day + '日'
  141. // } else { // 返回相差年数月数天数时分秒
  142. // return year + '年' + month + '月' + day + '日' + hour + '时' + minute + '分' + second + '秒'
  143. // }
  144. }