monkeeShark/src/prelude/time.ts

30 lines
865 B
TypeScript
Raw Normal View History

const dateTimeIntervals = {
2020-03-06 16:04:36 +00:00
'day': 86400000,
'hour': 3600000,
2020-03-07 02:23:31 +00:00
'ms': 1,
};
2020-03-07 02:23:31 +00:00
export function dateUTC(time: number[]): Date {
return new Date(Date.UTC(time[0], time[1], time[2], time[3], time[4], time[5], time[6]));
}
export function isTimeSame(a: Date, b: Date): boolean {
2020-02-29 07:38:07 +00:00
return a.getTime() === b.getTime();
}
export function isTimeBefore(a: Date, b: Date): boolean {
return (a.getTime() - b.getTime()) < 0;
}
export function isTimeAfter(a: Date, b: Date): boolean {
return (a.getTime() - b.getTime()) > 0;
}
2020-03-07 02:23:31 +00:00
export function addTime(x: Date, value: number, span: keyof typeof dateTimeIntervals = 'ms'): Date {
return new Date(x.getTime() + (value * dateTimeIntervals[span]));
}
2020-03-07 02:23:31 +00:00
export function subtractTime(x: Date, value: number, span: keyof typeof dateTimeIntervals = 'ms'): Date {
return new Date(x.getTime() - (value * dateTimeIntervals[span]));
}