这是一个支持天、小时、分钟和秒的时间跨度类的类似 .NET C# 实现。此实现还支持负时间跨度。
const MILLIS_PER_SECOND = 1000;
const MILLIS_PER_MINUTE = MILLIS_PER_SECOND * 60; // 60,000
const MILLIS_PER_HOUR = MILLIS_PER_MINUTE * 60; // 3,600,000
const MILLIS_PER_DAY = MILLIS_PER_HOUR * 24; // 86,400,000
export class TimeSpan {
private _millis: number;
private static interval(value: number, scale: number): TimeSpan {
if (Number.isNaN(value)) {
throw new Error("value can't be NaN");
}
const tmp = value * scale;
const millis = TimeSpan.round(tmp + (value >= 0 ? 0.5 : -0.5));
if ((millis > TimeSpan.maxValue.totalMilliseconds) || (millis < TimeSpan.minValue.totalMilliseconds)) {
throw new TimeSpanOverflowError("TimeSpanTooLong");
}
return new TimeSpan(millis);
}
private static round(n: number): number {
if (n < 0) {
return Math.ceil(n);
} else if (n > 0) {
return Math.floor(n);
}
return 0;
}
private static timeToMilliseconds(hour: number, minute: number, second: number): number {
const totalSeconds = (hour * 3600) + (minute * 60) + second;
if (totalSeconds > TimeSpan.maxValue.totalSeconds || totalSeconds < TimeSpan.minValue.totalSeconds) {
throw new TimeSpanOverflowError("TimeSpanTooLong");
}
return totalSeconds * MILLIS_PER_SECOND;
}
public static get zero(): TimeSpan {
return new TimeSpan(0);
}
public static get maxValue(): TimeSpan {
return new TimeSpan(Number.MAX_SAFE_INTEGER);
}
public static get minValue(): TimeSpan {
return new TimeSpan(Number.MIN_SAFE_INTEGER);
}
public static fromDays(value: number): TimeSpan {
return TimeSpan.interval(value, MILLIS_PER_DAY);
}
public static fromHours(value: number): TimeSpan {
return TimeSpan.interval(value, MILLIS_PER_HOUR);
}
public static fromMilliseconds(value: number): TimeSpan {
return TimeSpan.interval(value, 1);
}
public static fromMinutes(value: number): TimeSpan {
return TimeSpan.interval(value, MILLIS_PER_MINUTE);
}
public static fromSeconds(value: number): TimeSpan {
return TimeSpan.interval(value, MILLIS_PER_SECOND);
}
public static fromTime(hours: number, minutes: number, seconds: number): TimeSpan;
public static fromTime(days: number, hours: number, minutes: number, seconds: number, milliseconds: number): TimeSpan;
public static fromTime(daysOrHours: number, hoursOrMinutes: number, minutesOrSeconds: number, seconds?: number, milliseconds?: number): TimeSpan {
if (milliseconds != undefined) {
return this.fromTimeStartingFromDays(daysOrHours, hoursOrMinutes, minutesOrSeconds, seconds, milliseconds);
} else {
return this.fromTimeStartingFromHours(daysOrHours, hoursOrMinutes, minutesOrSeconds);
}
}
private static fromTimeStartingFromHours(hours: number, minutes: number, seconds: number): TimeSpan {
const millis = TimeSpan.timeToMilliseconds(hours, minutes, seconds);
return new TimeSpan(millis);
}
private static fromTimeStartingFromDays(days: number, hours: number, minutes: number, seconds: number, milliseconds: number): TimeSpan {
const totalMilliSeconds = (days * MILLIS_PER_DAY) +
(hours * MILLIS_PER_HOUR) +
(minutes * MILLIS_PER_MINUTE) +
(seconds * MILLIS_PER_SECOND) +
milliseconds;
if (totalMilliSeconds > TimeSpan.maxValue.totalMilliseconds || totalMilliSeconds < TimeSpan.minValue.totalMilliseconds) {
throw new TimeSpanOverflowError("TimeSpanTooLong");
}
return new TimeSpan(totalMilliSeconds);
}
constructor(millis: number) {
this._millis = millis;
}
public get days(): number {
return TimeSpan.round(this._millis / MILLIS_PER_DAY);
}
public get hours(): number {
return TimeSpan.round((this._millis / MILLIS_PER_HOUR) % 24);
}
public get minutes(): number {
return TimeSpan.round((this._millis / MILLIS_PER_MINUTE) % 60);
}
public get seconds(): number {
return TimeSpan.round((this._millis / MILLIS_PER_SECOND) % 60);
}
public get milliseconds(): number {
return TimeSpan.round(this._millis % 1000);
}
public get totalDays(): number {
return this._millis / MILLIS_PER_DAY;
}
public get totalHours(): number {
return this._millis / MILLIS_PER_HOUR;
}
public get totalMinutes(): number {
return this._millis / MILLIS_PER_MINUTE;
}
public get totalSeconds(): number {
return this._millis / MILLIS_PER_SECOND;
}
public get totalMilliseconds(): number {
return this._millis;
}
public add(ts: TimeSpan): TimeSpan {
const result = this._millis + ts.totalMilliseconds;
return new TimeSpan(result);
}
public subtract(ts: TimeSpan): TimeSpan {
const result = this._millis - ts.totalMilliseconds;
return new TimeSpan(result);
}
}
如何使用
创建一个新的 TimeSpan 对象
从零开始
const ts = TimeSpan.zero;
从毫秒
const milliseconds = 10000; // 1 second
// by using the constructor
const ts1 = new TimeSpan(milliseconds);
// or as an alternative you can use the static factory method
const ts2 = TimeSpan.fromMilliseconds(milliseconds);
从秒
const seconds = 86400; // 1 day
const ts = TimeSpan.fromSeconds(seconds);
从分钟
const minutes = 1440; // 1 day
const ts = TimeSpan.fromMinutes(minutes);
从小时
const hours = 24; // 1 day
const ts = TimeSpan.fromHours(hours);
从天
const days = 1; // 1 day
const ts = TimeSpan.fromDays(days);
从具有给定小时、分钟和秒的时间开始
const hours = 1;
const minutes = 1;
const seconds = 1;
const ts = TimeSpan.fromTime(hours, minutes, seconds);
从 time2 开始,具有给定的天、小时、分钟、秒和毫秒
const days = 1;
const hours = 1;
const minutes = 1;
const seconds = 1;
const milliseconds = 1;
const ts = TimeSpan.fromTime(days, hours, minutes, seconds, milliseconds);
从最大安全整数
const ts = TimeSpan.maxValue;
从最小安全整数
const ts = TimeSpan.minValue;
从最小安全整数
const ts = TimeSpan.minValue;
添加
const ts1 = TimeSpan.fromDays(1);
const ts2 = TimeSpan.fromHours(1);
const ts = ts1.add(ts2);
console.log(ts.days); // 1
console.log(ts.hours); // 1
console.log(ts.minutes); // 0
console.log(ts.seconds); // 0
console.log(ts.milliseconds); // 0
减去
const ts1 = TimeSpan.fromDays(1);
const ts2 = TimeSpan.fromHours(1);
const ts = ts1.subtract(ts2);
console.log(ts.days); // 0
console.log(ts.hours); // 23
console.log(ts.minutes); // 0
console.log(ts.seconds); // 0
console.log(ts.milliseconds); // 0
获取间隔
const days = 1;
const hours = 1;
const minutes = 1;
const seconds = 1;
const milliseconds = 1;
const ts = TimeSpan.fromTime2(days, hours, minutes, seconds, milliseconds);
console.log(ts.days); // 1
console.log(ts.hours); // 1
console.log(ts.minutes); // 1
console.log(ts.seconds); // 1
console.log(ts.milliseconds); // 1
console.log(ts.totalDays) // 1.0423726967592593;
console.log(ts.totalHours) // 25.016944722222224;
console.log(ts.totalMinutes) // 1501.0166833333333;
console.log(ts.totalSeconds) // 90061.001;
console.log(ts.totalMilliseconds); // 90061001;
另请参阅:
https ://github.com/erdas/timespan