1
1
mirror of https://github.com/theoludwig/p61-project.git synced 2024-07-17 07:00:12 +02:00
p61-project/utils/dates.ts

49 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

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-05-02 01:08:27 +02:00
export const getNowDateUTC = (): Date => {
2024-04-09 23:53:55 +02:00
const date = new Date()
const milliseconds = Date.UTC(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
)
return new Date(milliseconds)
}
/**
* 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
*/
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,
)
)
}