import { formatInTimeZone } from 'date-fns-tz'; export class DateTimeConstants { static readonly DAYS_IN_WEEK: number = 7; static readonly HOURS_IN_DAY: number = 24; static readonly DAY_INTERVAL: number = 24 * 60 * 60 * 1000; static readonly HOUR_INTERVAL: number = 60 * 60 * 1000; } export function atEndOfDay(date: Date): Date { let endOfDay = new Date(date); endOfDay.setHours(23, 59, 59, 999); return endOfDay; } export function atStartOfDay(date: Date): Date { let startOfDay = new Date(date); startOfDay.setHours(0, 0, 0, 0); return startOfDay; } export function atEndOfHour(date: Date): Date { let endOfHour = new Date(date); endOfHour.setMinutes(59, 59, 999); return endOfHour; } export function atStartOfHour(date: Date): Date { let startOfHour = new Date(date); startOfHour.setMinutes(0, 0, 0); return startOfHour; } /** Clears Daylight saving time adjustment from the given time. */ export function toNormalDate(zonedDate: Date, adjustment: number): Date { return new Date(zonedDate.getTime() - adjustment); } export function adjustToMiddleOfDay(zonedDate: Date, timeZone: string): Date { let hourStr: string = formatInTimeZone(zonedDate, timeZone, "HH"); let h: number = parseInt(hourStr); let addHours: number = 12 - h; return new Date(zonedDate.getTime() + (addHours * DateTimeConstants.HOUR_INTERVAL)); } export function getDSTAdjustedDate(previousIsDST: boolean, zonedDate: Date, dstAdjustment: number): Date { // adjusts previously without dst adjusted date by dst // ((date + interval) - dst ) // Note! intervals that are less or equal to dst are not supported // currently. let isDST: boolean = dstAdjustment > 0; if (previousIsDST && !isDST) { // previously added interval is shorter than the real interval. // with 24h interval and 1h dst: real interval is 25h. return new Date(zonedDate.getTime() + dstAdjustment); } else if (!previousIsDST && isDST) { // previously added interval is longer than the real interval. // with 24h interval and 1h dst: real interval is 23h. return new Date(zonedDate.getTime() - dstAdjustment); } return zonedDate; }