| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /**
- * @description 时间工具类
- * @author 邹勇
- */
- export default class TimeUtil {
- constructor() {
- }
- /**
- * 本地时间与服务器时间的差值
- */
- private offTime: number = 0;
- /**
- * 设置服务器时间
- * @param time 服务器时间戳(秒)
- */
- public setServerTime(time: number) {
- this.offTime = time - Math.ceil(new Date().getTime() / 1000);
- }
- /**
- * 获取当前服务器时间,如果没有设置过服务器时间,默认为本地时间
- * @returns 当前服务器时间戳(秒)
- */
- public getServerTime(): number {
- return Math.ceil(new Date().getTime() / 1000 + this.offTime);
- }
- /**
- * 倒计时转换为时间格式(h:m:s),没有单位
- * @param t 时间(秒)
- * @param f 格式,默认"h:m:s",可以过滤,如 "m:s"
- * @returns 规定格式的字符串 如"04:22:06"
- */
- public format(t: number, f: string = "h:m:s"): string {
- let d: number = Math.floor(t / 24 / 3600);
- let h: number = Math.floor((t / 3600) % 24);
- let m: number = Math.floor((t % 3600) / 60);
- let s: number = (t % 3600) % 60;
- function parse_format(t: number): string {
- let s: string = t.toString();
- if (t < 10) {
- s = "0" + t;
- }
- return s;
- }
- if (f.indexOf("d") != -1) {
- f = f.replace(/d/g, parse_format(d));
- }
- else {
- h += d * 24;
- }
- if (f.indexOf("h") != -1) {
- f = f.replace(/h/g, parse_format(h));
- }
- else {
- m += h * 60;
- }
- if (f.indexOf("m") != -1) {
- f = f.replace(/m/g, parse_format(m));
- }
- else {
- if (f.indexOf("h") != -1) {
- s += m * 60;
- } else {
- s = t;
- }
- }
- if (f.indexOf("s") != -1) {
- f = f.replace(/s/g, parse_format(s));
- }
- return f;
- }
- /**
- * 倒计时转换为时间格式,带中文单位 ,可过滤
- * @param t 时间(秒)
- * @param f 格式,默认"天时分秒",可以过滤,如 "分秒"
- * @returns 规定格式的字符串 如“2天13时11分05秒”
- */
- public formatWithUnit(t: number, f: string = "天时分秒"): string {
- let d: number = Math.floor(t / 24 / 3600);
- let h: number = Math.floor((t / 3600) % 24);
- let m: number = Math.floor((t % 3600) / 60);
- let s: number = (t % 3600) % 60;
- function parse_format(t: number, unit: string): string {
- let s: string = t.toString();
- if (t < 10) {
- s = "0" + t;
- }
- return s + unit;
- }
- let str = '';
- if (f.indexOf("天") != -1) {
- if (d > 0) {
- str += d + "天";
- }
- }
- else {
- h += d * 24;
- }
- if (f.indexOf("时") != -1) {
- if (h > 0) {
- // str += parse_format(h, "时");
- str += h + "时";
- }
- }
- else {
- m += h * 60;
- }
- if (f.indexOf("分") != -1) {
- if (m > 0) {
- // str += parse_format(m, "分");
- str += m + "分";
- }
- }
- else {
- s += m * 60;
- }
- if (f.indexOf("秒") != -1) {
- // str += parse_format(s, "秒");
- str += s + "秒";
- }
- return str;
- }
- /**
- * 获取一年中的第n天
- * @param t 时间戳(毫秒)
- * @returns 一年中的第n天
- */
- public getDayInYear(t: number): number {
- var time: Date = new Date(t);
- var month = time.getMonth() + 1;
- var year = time.getFullYear();
- var days = time.getDate();
- var sum = 0;
- let a = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
- let b = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
- if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
- for (var i: number = 0; i < month - 1; i++) {
- sum += b[i];
- }
- return sum + days;
- } else {
- for (var i: number = 0; i < month - 1; i++) {
- sum += a[i];
- }
- return sum + days;
- }
- }
- /**
- * 返回时间点在当天的秒数
- * @param date 时间
- * @returns 从零点开始的秒数
- */
- public getSecondsInDay(date: Date) {
- return date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds();
- }
- /**
- * 获取当前日期
- * @param data 1997年起的秒数
- * @param replace 间隔符,默认'-',则返回2020-10-10
- * @returns 当前时间字符串
- */
- public getNowDayString(data?: number, replace: string = '-'): string {
- let date = data ? new Date(data) : new Date();
- return date.getFullYear() + replace + (date.getMonth() + 1) + replace + date.getDate();
- }
- //等待seconds时间戳
- public WaitForSeconds(seconds: number) {
- return new Promise<void>(resolve => {
- setTimeout(() => resolve(), seconds * 1000);
- })
- }
- }
|