67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { MS_PER_DAY } from "src/modules/shared/constants/date-time.constant";
|
|
import { DAY_KEYS, DayKey } from "./timesheet.types";
|
|
|
|
export function toUTCDateOnly(date: Date | string): Date {
|
|
const d = new Date(date);
|
|
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
|
|
}
|
|
|
|
export function addDays(date:Date, days: number): Date {
|
|
return new Date(date.getTime() + days * MS_PER_DAY);
|
|
}
|
|
|
|
export function endOfDayUTC(date: Date | string): Date {
|
|
const d = toUTCDateOnly(date);
|
|
return new Date(d.getTime() + MS_PER_DAY - 1);
|
|
}
|
|
|
|
export function isBetweenUTC(date: Date, start: Date, end_inclusive: Date): boolean {
|
|
const time = date.getTime();
|
|
return time >= start.getTime() && time <= end_inclusive.getTime();
|
|
}
|
|
|
|
export function toTimeString(date: Date): string {
|
|
const hours = String(date.getUTCHours()).padStart(2,'0');
|
|
const minutes = String(date.getUTCMinutes()).padStart(2,'0');
|
|
return `${hours}:${minutes}`;
|
|
}
|
|
|
|
export function round2(num: number) {
|
|
return Math.round(num * 100) / 100;
|
|
}
|
|
|
|
export function shortDate(date:Date): string {
|
|
const mm = String(date.getUTCMonth()+1).padStart(2,'0');
|
|
const dd = String(date.getUTCDate()).padStart(2,'0');
|
|
return `${mm}/${dd}`;
|
|
}
|
|
|
|
export function dayKeyFromDate(date: Date, useUTC = true): DayKey {
|
|
const index = useUTC ? date.getUTCDay() : date.getDay(); // 0=Sunday..6=Saturday
|
|
return DAY_KEYS[index];
|
|
}
|
|
|
|
export const toHHmm = (date: Date) => date.toISOString().slice(11, 16);
|
|
|
|
export function parseISODate(iso: string): Date {
|
|
const [ y, m, d ] = iso.split('-').map(Number);
|
|
return new Date(y, (m ?? 1) - 1, d ?? 1);
|
|
}
|
|
|
|
export function parseHHmm(t: string): Date {
|
|
const [ hh, mm ] = t.split(':').map(Number);
|
|
return new Date(1970, 0, 1, hh || 0, mm || 0, 0, 0);
|
|
}
|
|
|
|
export const toNum = (value: any) =>
|
|
value && typeof value.toNumber === 'function' ? value.toNumber() :
|
|
typeof value === 'number' ? value :
|
|
value ? Number(value) : 0;
|
|
|
|
|
|
export const upper = (s?: string | null) => String(s ?? '').toUpperCase();
|
|
|
|
export const toRangeFromPeriod = (period: { period_start: Date; period_end: Date }) => ({
|
|
from: toUTCDateOnly(period.period_start),
|
|
to: endOfDayUTC(period.period_end),
|
|
}); |