-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
40 lines (35 loc) · 1.2 KB
/
index.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
/**
* Returns the day of the year.
* @param date - Date object.
* @param startAtZero - Whether the day of the year should be zero-based.
* @returns Day of year,
*/
export function getDayOfYear(date: Date, startAtZero = false): number {
const year = date.getFullYear()
const januaryFirst = new Date(year, 0, 1)
const givenDate = new Date(year, date.getMonth(), date.getDate())
return (
Math.round((givenDate.getTime() - januaryFirst.getTime()) / 86_400_000) +
(startAtZero ? 0 : 1)
)
}
/**
* Returns a date string in the modern Julian format. (i.e. YYYYDDD)
* ex. 2023-01-01 => '2023001'
* @param date - Date object.
* @returns Date string in modern Julian format
*/
export function toModernJulianDate(date: Date): string {
const dayOfYear = getDayOfYear(date)
return (date.getFullYear() * 1000 + dayOfYear).toString()
}
/**
* Returns a date string in the modern Julian format with a two-digit year. (i.e. YYDDD)
* ex. 2023-01-01 => '23001'
* @param date - Date object.
* @returns Date string in modern Julian format with a two-digit year
*/
export function toShortModernJulianDate(date: Date): string {
return toModernJulianDate(date).slice(2)
}
export default toModernJulianDate