TimeUtil.ts 6.4 KB

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