2019-07-24 18:36:48 +02:00
|
|
|
const dateTimeIntervals = {
|
2023-01-13 05:40:33 +01:00
|
|
|
day: 86400000,
|
|
|
|
hour: 3600000,
|
|
|
|
ms: 1,
|
2019-07-24 18:36:48 +02:00
|
|
|
};
|
|
|
|
|
2020-03-07 03:23:31 +01:00
|
|
|
export function dateUTC(time: number[]): Date {
|
2023-01-13 05:40:33 +01:00
|
|
|
const d =
|
|
|
|
time.length === 2
|
|
|
|
? Date.UTC(time[0], time[1])
|
|
|
|
: time.length === 3
|
2024-03-28 06:25:33 +01:00
|
|
|
? Date.UTC(time[0], time[1], time[2])
|
|
|
|
: time.length === 4
|
|
|
|
? Date.UTC(time[0], time[1], time[2], time[3])
|
|
|
|
: time.length === 5
|
|
|
|
? Date.UTC(time[0], time[1], time[2], time[3], time[4])
|
|
|
|
: time.length === 6
|
|
|
|
? Date.UTC(time[0], time[1], time[2], time[3], time[4], time[5])
|
|
|
|
: time.length === 7
|
|
|
|
? Date.UTC(
|
2024-02-11 18:50:57 +01:00
|
|
|
time[0],
|
|
|
|
time[1],
|
|
|
|
time[2],
|
|
|
|
time[3],
|
|
|
|
time[4],
|
|
|
|
time[5],
|
|
|
|
time[6],
|
2024-03-28 06:25:33 +01:00
|
|
|
)
|
|
|
|
: null;
|
2023-01-13 05:40:33 +01:00
|
|
|
|
|
|
|
if (!d) throw "wrong number of arguments";
|
2020-03-22 01:21:21 +01:00
|
|
|
|
|
|
|
return new Date(d);
|
2019-07-24 18:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isTimeSame(a: Date, b: Date): boolean {
|
2020-02-29 08:38:07 +01:00
|
|
|
return a.getTime() === b.getTime();
|
2019-07-24 18:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isTimeBefore(a: Date, b: Date): boolean {
|
2023-01-13 05:40:33 +01:00
|
|
|
return a.getTime() - b.getTime() < 0;
|
2019-07-24 18:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isTimeAfter(a: Date, b: Date): boolean {
|
2023-01-13 05:40:33 +01:00
|
|
|
return a.getTime() - b.getTime() > 0;
|
2019-07-24 18:36:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export function addTime(
|
|
|
|
x: Date,
|
|
|
|
value: number,
|
|
|
|
span: keyof typeof dateTimeIntervals = "ms",
|
|
|
|
): Date {
|
|
|
|
return new Date(x.getTime() + value * dateTimeIntervals[span]);
|
2019-07-24 18:36:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export function subtractTime(
|
|
|
|
x: Date,
|
|
|
|
value: number,
|
|
|
|
span: keyof typeof dateTimeIntervals = "ms",
|
|
|
|
): Date {
|
|
|
|
return new Date(x.getTime() - value * dateTimeIntervals[span]);
|
2019-07-24 18:36:48 +02:00
|
|
|
}
|