TimeUtil.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { StringUtil } from "./StringUtil";
  2. export class TimeUtil {
  3. //获取时间戳
  4. static getTimeStamp() {
  5. return Date.now()
  6. }
  7. //毫秒
  8. static getTime() {
  9. return Number(new Date().getTime());
  10. }
  11. //秒
  12. static getTimeEx(D_value) {
  13. return Math.floor(Number(new Date().getTime()) / 1000) + D_value;
  14. }
  15. //毫秒
  16. static getTimeEx2(D_value) {
  17. return Math.floor(Number(new Date().getTime())) + D_value;
  18. }
  19. //格式化时间(秒数到01:23:45)
  20. static formatTime(time: number) {
  21. if (time <= 0) return "";
  22. let h = Math.floor(time / 3600);
  23. let m = Math.floor((time / 60 % 60));
  24. let s = Math.floor((time % 60));
  25. let str: any = ((h < 10) ? "0{0}:" : "{0}:") + ((m < 10) ? "0{1}:" : "{1}:") + ((s < 10) ? "0{2}" : "{2}");
  26. return StringUtil.format(str, h, m, s);
  27. }
  28. //格式化时间(00:00)
  29. static formatTimeByTwo(time: number) {
  30. if (time <= 0) return "";
  31. let h = Math.floor(time / 3600);
  32. let m = Math.floor((time / 60 % 60));
  33. let s = Math.floor((time % 60));
  34. if (h > 0) {
  35. let str: any = ((h < 10) ? "0{0}:" : "{0}:") + ((m < 10) ? "0{1}" : "{1}")
  36. return StringUtil.format(str, h, m);
  37. } else {
  38. let str: any = ((m < 10) ? "0{0}:" : "{0}:") + ((s < 10) ? "0{1}" : "{1}");
  39. return StringUtil.format(str, m, s);
  40. }
  41. }
  42. static getDateDay(time, half?) {
  43. let date = new Date(time * 1000);
  44. let Y = date.getFullYear() + '-'
  45. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  46. let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ''
  47. if (half) {
  48. return Y + M + D;
  49. }
  50. let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  51. let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  52. let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
  53. return Y + M + D + ' ' + h + m + s
  54. }
  55. //日/月 时:分 (毫秒制)
  56. static getDate_DDMMHHMM_ms(time) {
  57. let date = new Date(time);
  58. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + ''
  59. let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '/'
  60. let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  61. let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
  62. return D + M + ' ' + h + m
  63. }
  64. static getDateDay_DDMMYYYY_hhmm(time) {
  65. let date = new Date(time);
  66. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  67. let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '-'
  68. let Y = date.getFullYear() + ''
  69. let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  70. let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ''
  71. return D + M + Y + ' ' + h + m
  72. }
  73. //日/月 (毫秒制)
  74. static getDate_DDMM_ms(time) {
  75. let date = new Date(time);
  76. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + ''
  77. let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '/'
  78. return D + M
  79. }
  80. //两个时间间隔天数
  81. static getDayInterval(time1: number, time2: number) {
  82. let format_time = (count: number) => {
  83. let time = new Date(count);
  84. let year = time.getFullYear();
  85. let month = time.getMonth() + 1;
  86. let date = time.getDate();
  87. return (year + "-" + month + "-" + date)
  88. };
  89. return (Date.parse(format_time(Math.max(time1, time2))) - Date.parse(format_time(Math.min(time1, time2)))) / 86400000;
  90. }
  91. /**
  92. * 结束时间为跨天凌晨5点
  93. * @param serverTime
  94. * @returns
  95. */
  96. static getTodayEndTime(serverTime: number) {
  97. let time = new Date(serverTime * 1000);
  98. if (time.getHours() >= 5) {
  99. time.setHours(5, 0, 0, 0);
  100. return Number((time.getTime() + 86400000) / 1000);
  101. } else {
  102. time.setHours(5, 0, 0, 0);
  103. return Number(time.getTime() / 1000);
  104. }
  105. }
  106. /**
  107. * 时间格式化(倒计时)
  108. * @param seconds 要转换的时间
  109. * @param isShowDay 是否以:天/小时/分钟 的格式显示
  110. * @returns 返回当前传入时间对应的格式字符串
  111. */
  112. static formatCountdown(seconds: number, isShowDay: boolean) {
  113. let day = Math.floor(seconds / 60 / 60 / 24);
  114. let strReturn = '';
  115. if (isShowDay) {
  116. let hours = Math.floor((seconds % (60 * 60 * 24)) / 60 / 60);
  117. let minutes = Math.floor(seconds / 60 % 60);
  118. if (day >= 1) {
  119. strReturn = strReturn + day + '天';
  120. }
  121. strReturn = strReturn + hours + '时';
  122. strReturn = strReturn + minutes + '分';
  123. } else {
  124. let hours = Math.floor(seconds / 60 / 60);
  125. seconds = seconds - hours * 60 * 60;
  126. let minutes = Math.floor(seconds / 60);
  127. seconds = seconds - minutes * 60;
  128. strReturn = hours + '时', minutes + '分', seconds + '秒';
  129. }
  130. return strReturn;
  131. }
  132. /** 将秒转换为时分秒的时间格式 */
  133. static secondsToHMS(seconds: number, isShowHour?: boolean): string {
  134. if (!seconds) {
  135. seconds = 0;
  136. }
  137. const hours = Math.floor(seconds / 3600);
  138. const minutes = Math.floor((seconds % 3600) / 60);
  139. const remainingSeconds = seconds % 60;
  140. let timeStr = '';
  141. if (isShowHour) {
  142. // const hoursStr = hours < 10 ? "0" + hours : hours.toString();
  143. const hoursStr = hours.toString();
  144. const minutesStr = minutes < 10 ? "0" + minutes : minutes.toString();
  145. const secondsStr = remainingSeconds < 10 ? "0" + remainingSeconds : remainingSeconds.toString();
  146. timeStr = `${hoursStr}:${minutesStr}:${secondsStr}`;
  147. } else {
  148. const minutesStr = minutes < 10 ? "0" + minutes : minutes.toString();
  149. const secondsStr = remainingSeconds < 10 ? "0" + remainingSeconds : remainingSeconds.toString();
  150. timeStr = `${minutesStr}:${secondsStr}`;
  151. }
  152. return timeStr;
  153. }
  154. }