|
|
@@ -8,19 +8,30 @@ export default class TimeUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取当前日期
|
|
|
- * @param replace 间隔符,默认'-',则返回2020-10-10
|
|
|
- * @returns 当前时间字符串
|
|
|
+ * 本地时间与服务器时间的差值
|
|
|
*/
|
|
|
- public getNowDayString(replace: string = '-'): string {
|
|
|
- let date = new Date();
|
|
|
- return date.getFullYear() + replace + (date.getMonth() + 1) + replace + date.getDate();
|
|
|
+ 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) ,可自定义
|
|
|
+ * 倒计时转换为时间格式(h:m:s),没有单位
|
|
|
* @param t 时间(秒)
|
|
|
- * @param f 格式
|
|
|
+ * @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);
|
|
|
@@ -35,29 +46,130 @@ export default class TimeUtil {
|
|
|
}
|
|
|
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 {
|
|
|
+ }
|
|
|
+ else {
|
|
|
m += h * 60;
|
|
|
}
|
|
|
+
|
|
|
if (f.indexOf("m") != -1) {
|
|
|
f = f.replace(/m/g, parse_format(m));
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ 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) {
|
|
|
+ str += d + "天";
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ h += d * 24;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (f.indexOf("时") != -1) {
|
|
|
+ str += parse_format(h, "时");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ m += h * 60;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (f.indexOf("分") != -1) {
|
|
|
+ str += parse_format(m, "分");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ s += m * 60;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (f.indexOf("秒") != -1) {
|
|
|
+ str += parse_format(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 replace 间隔符,默认'-',则返回2020-10-10
|
|
|
+ * @returns 当前时间字符串
|
|
|
+ */
|
|
|
+ public getNowDayString(replace: string = '-'): string {
|
|
|
+ let date = new Date();
|
|
|
+ return date.getFullYear() + replace + (date.getMonth() + 1) + replace + date.getDate();
|
|
|
+ }
|
|
|
}
|