TimeUtil.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. str += d + "天";
  92. }
  93. else {
  94. h += d * 24;
  95. }
  96. if (f.indexOf("时") != -1) {
  97. str += parse_format(h, "时");
  98. }
  99. else {
  100. m += h * 60;
  101. }
  102. if (f.indexOf("分") != -1) {
  103. str += parse_format(m, "分");
  104. }
  105. else {
  106. s += m * 60;
  107. }
  108. if (f.indexOf("秒") != -1) {
  109. str += parse_format(s, "秒");
  110. }
  111. return str;
  112. }
  113. /**
  114. * 获取一年中的第n天
  115. * @param t 时间戳(毫秒)
  116. * @returns 一年中的第n天
  117. */
  118. public getDayInYear(t: number): number {
  119. var time: Date = new Date(t);
  120. var month = time.getMonth() + 1;
  121. var year = time.getFullYear();
  122. var days = time.getDate();
  123. var sum = 0;
  124. let a = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  125. let b = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  126. if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
  127. for (var i: number = 0; i < month - 1; i++) {
  128. sum += b[i];
  129. }
  130. return sum + days;
  131. } else {
  132. for (var i: number = 0; i < month - 1; i++) {
  133. sum += a[i];
  134. }
  135. return sum + days;
  136. }
  137. }
  138. /**
  139. * 返回时间点在当天的秒数
  140. * @param date 时间
  141. * @returns 从零点开始的秒数
  142. */
  143. public getSecondsInDay(date: Date) {
  144. return date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds();
  145. }
  146. /**
  147. * 获取当前日期
  148. * @param data 1997年起的秒数
  149. * @param replace 间隔符,默认'-',则返回2020-10-10
  150. * @returns 当前时间字符串
  151. */
  152. public getNowDayString(data?: number, replace: string = '-'): string {
  153. let date = data ? new Date(data) : new Date();
  154. return date.getFullYear() + replace + (date.getMonth() + 1) + replace + date.getDate();
  155. }
  156. //等待seconds时间戳
  157. public WaitForSeconds(seconds: number) {
  158. return new Promise<void>(resolve => {
  159. setTimeout(() => resolve(), seconds * 1000);
  160. })
  161. }
  162. }