2024-04-08 23:21:36 +02:00
|
|
|
export const ONE_DAY_MILLISECONDS = 1_000 * 60 * 60 * 24
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a date as a string value in ISO format (without time information).
|
|
|
|
*
|
|
|
|
* @param date
|
|
|
|
* @returns
|
|
|
|
* @example getISODate(new Date("2012-05-23")) // "2012-05-23"
|
|
|
|
*/
|
|
|
|
export const getISODate = (date: Date): string => {
|
|
|
|
return date.toISOString().slice(0, 10)
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:53:55 +02:00
|
|
|
export const getNowDate = (): Date => {
|
|
|
|
const date = new Date()
|
|
|
|
const milliseconds = Date.UTC(
|
|
|
|
date.getFullYear(),
|
|
|
|
date.getMonth(),
|
|
|
|
date.getDate(),
|
|
|
|
date.getHours(),
|
|
|
|
date.getMinutes(),
|
|
|
|
date.getSeconds(),
|
|
|
|
)
|
|
|
|
return new Date(milliseconds)
|
|
|
|
}
|
|
|
|
|
2024-04-08 23:21:36 +02:00
|
|
|
/**
|
|
|
|
* Get the week number [1-52] for a given date.
|
|
|
|
* @param {Date} date
|
|
|
|
* @returns {number}
|
2024-05-01 14:03:25 +02:00
|
|
|
* @example getWeekNumber(new Date("2020-01-01")) // 1
|
|
|
|
* @example getWeekNumber(new Date("2020-01-08")) // 2
|
2024-04-08 23:21:36 +02:00
|
|
|
*/
|
|
|
|
export const getWeekNumber = (date: Date): number => {
|
|
|
|
const dateCopy = new Date(date.getTime())
|
|
|
|
dateCopy.setHours(0, 0, 0, 0)
|
|
|
|
dateCopy.setDate(dateCopy.getDate() + 3 - ((dateCopy.getDay() + 6) % 7))
|
|
|
|
const week1 = new Date(dateCopy.getFullYear(), 0, 4)
|
|
|
|
return (
|
|
|
|
1 +
|
|
|
|
Math.round(
|
|
|
|
((dateCopy.getTime() - week1.getTime()) / ONE_DAY_MILLISECONDS -
|
|
|
|
3 +
|
|
|
|
((week1.getDay() + 6) % 7)) /
|
|
|
|
7,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|