dateFormat.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*若小于10,前面加0*/
  2. function isZero(m) {
  3. return m < 10 ? '0' + m : m
  4. }
  5. function friendlyDate(timestamp) {
  6. let formats = {
  7. 'year': '%n% 年前',
  8. 'month': '%n% 月前',
  9. 'day': '%n% 天前',
  10. 'hour': '%n% 小时前',
  11. 'minute': '%n% 分钟前',
  12. 'second': '%n% 秒前',
  13. };
  14. let now = Date.now();
  15. let seconds = Math.floor((now - timestamp) / 1000);
  16. let minutes = Math.floor(seconds / 60);
  17. let hours = Math.floor(minutes / 60);
  18. let days = Math.floor(hours / 24);
  19. let months = Math.floor(days / 30);
  20. let years = Math.floor(months / 12);
  21. let diffType = '';
  22. let diffValue = 0;
  23. if (years > 0) {
  24. diffType = 'year';
  25. diffValue = years;
  26. } else {
  27. if (months > 0) {
  28. diffType = 'month';
  29. diffValue = months;
  30. } else {
  31. if (days > 0) {
  32. diffType = 'day';
  33. diffValue = days;
  34. } else {
  35. if (hours > 0) {
  36. diffType = 'hour';
  37. diffValue = hours;
  38. } else {
  39. if (minutes > 0) {
  40. diffType = 'minute';
  41. diffValue = minutes;
  42. } else {
  43. diffType = 'second';
  44. diffValue = seconds === 0 ? (seconds = 1) : seconds;
  45. }
  46. }
  47. }
  48. }
  49. }
  50. return formats[diffType].replace('%n%', diffValue);
  51. }
  52. /*
  53. * 时间格式转换
  54. * @param timestamp 时间 String || Date (123456 && 1978-10-25 10:25:20 && data对象)
  55. * @param format 输出格式 String (slash: 1978/10/25 10:25:20 horizontal: 1978-10-25 10:25:20 minute:10:25 second:10:25)
  56. */
  57. function formatDate(timestamp, format = 'slash') {
  58. //时间戳是整数,否则要parseInt转换
  59. let time = '';
  60. console.log(timestamp.constructor);
  61. if (timestamp.constructor === Date) {
  62. time = timestamp;
  63. } else {
  64. time = (parseFloat(timestamp) + 946684800).toString();
  65. if (time.indexOf('-') != -1 || time.indexOf('/') != -1 || time.indexOf(':') != -1) {
  66. //将时间字符串转成时间戳
  67. time = time.replace(/-/g, '/') //为了兼容IOS,需先将字符串转换为'2018/9/11 9:11:23'
  68. time = new Date(time);
  69. } else {
  70. if (time.length == 10) {
  71. time = new Date(parseInt(time) * 1000);
  72. } else if (time.length == 13) {
  73. time = new Date(parseInt(time));
  74. }
  75. }
  76. }
  77. let y = time.getFullYear();
  78. let m = time.getMonth() + 1;
  79. let d = time.getDate();
  80. let h = time.getHours();
  81. let mm = time.getMinutes();
  82. let s = time.getSeconds();
  83. let now = new Date();
  84. let seconds = Math.floor((now - time));
  85. let minutes = Math.floor(seconds / 60);
  86. let hours = Math.floor(minutes / 60);
  87. let days = Math.floor(hours / 24);
  88. let months = Math.floor(days / 30);
  89. let years = Math.floor(months / 12);
  90. let NowY = (new Date()).getFullYear();
  91. let dayTime = 24 * 60 * 60;
  92. //当前时间
  93. let td = new Date();
  94. td = new Date(td.getFullYear(), td.getMonth(), td.getDate());
  95. //传递时间
  96. let od = new Date(time);
  97. od = new Date(od.getFullYear(), od.getMonth(), od.getDate());
  98. //当前时间与传递时间的差值
  99. let xc = (od - td) / 1000 / 60 / 60 / 24;
  100. //return y+'/'+isZero(m)+'/'+isZero(d)+' '+isZero(h)+':'+isZero(mm)+':'+isZero(s);
  101. switch (format) {
  102. case "horizontal":
  103. return y + '-' + isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s);
  104. break;
  105. case "day":
  106. return y + '-' + isZero(m) + '-' + isZero(d);
  107. break;
  108. case "minute":
  109. let result = '';
  110. if (NowY === y) {
  111. if (xc < -1) {
  112. result = isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm)
  113. } else if (xc < 0) {
  114. result = '昨天' + ' ' + isZero(h) + ':' + isZero(mm)
  115. } else if (xc == 0) {
  116. result = isZero(h) + ':' + isZero(mm)
  117. };
  118. } else {
  119. result = y + '-' + isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm)
  120. }
  121. return result;
  122. break;
  123. case "second":
  124. let result2 = '';
  125. if (NowY === y) {
  126. if (xc < -1) {
  127. result2 = isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
  128. } else if (xc < 0) {
  129. result2 = '昨天' + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
  130. } else if (xc == 0) {
  131. result2 = isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
  132. };
  133. } else {
  134. result2 = y + '-' + isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
  135. }
  136. return result2;
  137. break;
  138. case "spot":
  139. let result3 = '';
  140. result3 = y + '.' + isZero(m) + '.' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
  141. return result3;
  142. break;
  143. case "date":
  144. let result4 = '';
  145. result4 = y + '.' + isZero(m) + '.' + isZero(d);
  146. return result4;
  147. break;
  148. case "time":
  149. let result5 = '';
  150. result5 = isZero(h) + ':' + isZero(mm);
  151. return result5;
  152. break;
  153. case "dateTime":
  154. let result6 = '';
  155. result6 = y + '-' + isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm);
  156. return result6;
  157. break;
  158. case "bvcTime":
  159. return isZero(h) + ':' + isZero(mm) + ':' + isZero(s) + ' ' + isZero(d) + '/' + isZero(m) + '/' + y;
  160. break;
  161. default:
  162. return y + '/' + isZero(m) + '/' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s);
  163. }
  164. }
  165. export {
  166. friendlyDate,
  167. formatDate
  168. }