TimeUtil.ts 6.5 KB

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