TimeUtil.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @description 时间工具类
  3. * @author 邹勇
  4. */
  5. export default class TimeUtil {
  6. constructor() {
  7. }
  8. /**
  9. * 本地时间与服务器时间的差值
  10. */
  11. private offTime: number = 0;
  12. /**
  13. * 设置服务器时间
  14. * @param time 服务器时间戳(秒)
  15. */
  16. public setServerTime(time: number) {
  17. this.offTime = time - Math.ceil(new Date().getTime() / 1000);
  18. }
  19. /**
  20. * 获取当前服务器时间,如果没有设置过服务器时间,默认为本地时间
  21. * @returns 当前服务器时间戳(秒)
  22. */
  23. public getServerTime(): number {
  24. return Math.ceil(new Date().getTime() / 1000 + this.offTime);
  25. }
  26. /**
  27. * 倒计时转换为时间格式(h:m:s),没有单位
  28. * @param t 时间(秒)
  29. * @param f 格式,默认"h:m:s",可以过滤,如 "m:s"
  30. * @returns 规定格式的字符串 如"04:22:06"
  31. */
  32. public format(t: number, f: string = "h:m:s"): string {
  33. let d: number = Math.floor(t / 24 / 3600);
  34. let h: number = Math.floor((t / 3600) % 24);
  35. let m: number = Math.floor((t % 3600) / 60);
  36. let s: number = (t % 3600) % 60;
  37. function parse_format(t: number): string {
  38. let s: string = t.toString();
  39. if (t < 10) {
  40. s = "0" + t;
  41. }
  42. return s;
  43. }
  44. if (f.indexOf("d") != -1) {
  45. f = f.replace(/d/g, parse_format(d));
  46. }
  47. else {
  48. h += d * 24;
  49. }
  50. if (f.indexOf("h") != -1) {
  51. f = f.replace(/h/g, parse_format(h));
  52. }
  53. else {
  54. m += h * 60;
  55. }
  56. if (f.indexOf("m") != -1) {
  57. f = f.replace(/m/g, parse_format(m));
  58. }
  59. else {
  60. if (f.indexOf("h") != -1) {
  61. s += m * 60;
  62. } else {
  63. s = t;
  64. }
  65. }
  66. if (f.indexOf("s") != -1) {
  67. f = f.replace(/s/g, parse_format(s));
  68. }
  69. return f;
  70. }
  71. /**
  72. * 倒计时转换为时间格式,带中文单位 ,可过滤
  73. * @param t 时间(秒)
  74. * @param f 格式,默认"天时分秒",可以过滤,如 "分秒"
  75. * @returns 规定格式的字符串 如“2天13时11分05秒”
  76. */
  77. public formatWithUnit(t: number, f: string = "天时分秒"): string {
  78. let d: number = Math.floor(t / 24 / 3600);
  79. let h: number = Math.floor((t / 3600) % 24);
  80. let m: number = Math.floor((t % 3600) / 60);
  81. let s: number = (t % 3600) % 60;
  82. function parse_format(t: number, unit: string): string {
  83. let s: string = t.toString();
  84. if (t < 10) {
  85. s = "0" + t;
  86. }
  87. return s + unit;
  88. }
  89. let str = '';
  90. if (f.indexOf("天") != -1) {
  91. if (d > 0) {
  92. str += d + "天";
  93. }
  94. }
  95. else {
  96. h += d * 24;
  97. }
  98. if (f.indexOf("时") != -1) {
  99. if (h > 0) {
  100. // str += parse_format(h, "时");
  101. str += h + "时";
  102. }
  103. }
  104. else {
  105. m += h * 60;
  106. }
  107. if (f.indexOf("分") != -1) {
  108. if (m > 0) {
  109. // str += parse_format(m, "分");
  110. str += m + "分";
  111. }
  112. }
  113. else {
  114. s += m * 60;
  115. }
  116. if (f.indexOf("秒") != -1) {
  117. // str += parse_format(s, "秒");
  118. str += s + "秒";
  119. }
  120. return str;
  121. }
  122. /**
  123. * 获取一年中的第n天
  124. * @param t 时间戳(毫秒)
  125. * @returns 一年中的第n天
  126. */
  127. public getDayInYear(t: number): number {
  128. var time: Date = new Date(t);
  129. var month = time.getMonth() + 1;
  130. var year = time.getFullYear();
  131. var days = time.getDate();
  132. var sum = 0;
  133. let a = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  134. let b = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  135. if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
  136. for (var i: number = 0; i < month - 1; i++) {
  137. sum += b[i];
  138. }
  139. return sum + days;
  140. } else {
  141. for (var i: number = 0; i < month - 1; i++) {
  142. sum += a[i];
  143. }
  144. return sum + days;
  145. }
  146. }
  147. /**
  148. * 返回时间点在当天的秒数
  149. * @param date 时间
  150. * @returns 从零点开始的秒数
  151. */
  152. public getSecondsInDay(date: Date) {
  153. return date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds();
  154. }
  155. /**
  156. * 获取当前日期
  157. * @param data 1997年起的秒数
  158. * @param replace 间隔符,默认'-',则返回2020-10-10
  159. * @returns 当前时间字符串
  160. */
  161. public getNowDayString(data?: number, replace: string = '-'): string {
  162. let date = data ? new Date(data) : new Date();
  163. return date.getFullYear() + replace + (date.getMonth() + 1) + replace + date.getDate();
  164. }
  165. //等待seconds时间戳
  166. public WaitForSeconds(seconds: number) {
  167. return new Promise<void>(resolve => {
  168. setTimeout(() => resolve(), seconds * 1000);
  169. })
  170. }
  171. }