utils.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * 格式化日期
  3. * @prama t 时间戳
  4. * @return str MM-dd HH:mm
  5. */
  6. export function formatDate(t) {
  7. t = t || Date.now();
  8. let time = new Date(t);
  9. let str = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
  10. str += '-';
  11. str += time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
  12. str += ' ';
  13. str += time.getHours();
  14. str += ':';
  15. str += time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
  16. return str;
  17. };
  18. /**
  19. * 距当前时间点的时长
  20. * @prama time 13位时间戳
  21. * @return str x秒 / x分钟 / x小时
  22. */
  23. export function formateTime(time) {
  24. const second = 1000;
  25. const minute = second * 60;
  26. const hour = minute * 60;
  27. const day = hour * 24;
  28. const now = new Date().getTime();
  29. const diffValue = now - time;
  30. // 计算差异时间的量级
  31. const secondC = diffValue / second;
  32. const minC = diffValue / minute;
  33. const hourC = diffValue / hour;
  34. const dayC = diffValue / day;
  35. if (dayC >= 1) {
  36. return parseInt(dayC) + "天";
  37. } else if (hourC >= 1) {
  38. return parseInt(hourC) + "小时";
  39. } else if (minC >= 1) {
  40. return parseInt(minC) + "分钟";
  41. } else if (secondC >= 1) {
  42. return parseInt(secondC) + "秒";
  43. } else {
  44. return '0秒';
  45. }
  46. }