This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DateUtils.ts
111 lines (102 loc) · 4.03 KB
/
DateUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2023. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
/**
* Pure zero-dep JavaScript date operations.
*/
export class DateUtils {
/**
* Creates a new Date object that is a copy of the provided date.
*
* @param {Date} date - The original Date object to be cloned.
* @returns {Date} A new Date object that is a copy of the original date.
*/
public static cloneDate (date: Date) : Date {
return new Date(date);
}
/**
* Subtracts a specified number of days from a given date.
*
* Note! This handles times with local timezone.
*
* @param {Date} date - The original Date object.
* @param {number} offset - The number of days to subtract.
* @returns {Date} A new Date object resulting from subtracting the specified days from the original date.
*/
public static subtractDays (
date: Date,
offset: number,
) : Date {
const newDate = DateUtils.cloneDate(date);
newDate.setDate(newDate.getDate() - offset);
return newDate;
}
/**
* Gets the first day of the month based on the provided start date and offset.
*
* Note! This handles times with local timezone.
*
* @param {Date} startDate - The starting date from which to calculate the first day of the month.
* @param {number} offset - An optional offset to calculate the first day of a different month (default is 0).
* @returns {Date} The first day of the month as a new Date object.
*/
public static getFirstDayOfMonth (
startDate: Date,
offset: number = 0,
) : Date {
const nextMonthFirstDay = DateUtils.cloneDate(startDate);
nextMonthFirstDay.setMonth(nextMonthFirstDay.getMonth() + offset, 1);
return DateUtils.getFirstTimeOfDate(nextMonthFirstDay);
}
/**
* Gets the last day of the month based on the provided start date and offset.
*
* Note! This handles times with local timezone.
*
* @param {Date} startDate - The starting date from which to calculate the last day of the month.
* @param {number} offset - An optional offset to calculate the last day of a different month (default is 0).
* @returns {Date} The last day of the month as a new Date object with the time set to 23:59:59.999.
*/
public static getLastDayOfMonth (
startDate: Date,
offset: number = 0,
) : Date {
const firstDayOfMonth = DateUtils.getFirstDayOfMonth(startDate, offset + 1);
// Subtract one day to get the last day of the month
const lastDayOfMonth = DateUtils.subtractDays(firstDayOfMonth, 1);
return DateUtils.getLastTimeOfDate(lastDayOfMonth);
}
/**
* Gets the last time of the day (23:59:59.999) for a given date.
*
* Note! This handles times with local timezone.
*
* @param {Date} date - The input date.
* @returns {Date} A new Date object representing the last time (23:59:59.999) of the given date.
*/
public static getLastTimeOfDate (date: Date): Date {
const lastTime = DateUtils.cloneDate(date);
lastTime.setHours(23, 59, 59, 999);
return lastTime;
}
/**
* Gets the first time of the day (00:00:00.000) for a given date.
*
* Note! This handles times with local timezone.
*
* @param {Date} date - The input date.
* @returns {Date} A new Date object representing the last time (00:00:00.000) of the given date.
*/
public static getFirstTimeOfDate (date: Date): Date {
const lastTime = DateUtils.cloneDate(date);
lastTime.setHours(0, 0, 0, 0);
return lastTime;
}
/**
* Gets the number of microseconds since January 1, 1970 (Unix Epoch) for the given date.
*
* @param {Date} value - The date for which to calculate the number of microseconds.
* @returns {number} The number of microseconds since January 1, 1970, for the given date.
*/
public static getMicroSeconds (value: Date) : number {
return value.getTime()*1000;
}
}