-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdates.ts
34 lines (28 loc) · 1.34 KB
/
dates.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
import utcToZonedTime from "date-fns-tz/utcToZonedTime";
import zonedTimeToUtc from "date-fns-tz/zonedTimeToUtc";
import { parseISO, startOfDay, endOfDay, formatISO, sub } from "date-fns";
import { months } from "$lib/constants/date";
export const getCurrentDateInTZ = (tz: string): string => {
const date = new Date();
const zoned = utcToZonedTime(date, tz);
return formatISO(zoned, { format: "extended", representation: "date" });
};
export const getDateSixMonthsAgoInTZ = (tz: string): string => {
const date = sub(new Date(), { months: 6 });
const zoned = utcToZonedTime(date, tz);
return formatISO(zoned, { format: "extended", representation: "date" });
};
export const getStartOfDayForDateInTZ = (dateString: string, tz: string): Date => {
const date = startOfDay(parseISO(dateString));
return zonedTimeToUtc(date, tz);
};
export const getEndOfDayForDateInTZ = (dateString: string, tz: string): Date => {
const date = endOfDay(parseISO(dateString));
return zonedTimeToUtc(date, tz);
};
// date-only fields in Contentful (fields without timestamps) still include UTC
// timezone, so we need to use getUTC* methods to generate a date string
export const getDateStringFromUTCDate = (dateString: string): string => {
const date = new Date(dateString);
return `${months[date.getUTCMonth()]} ${date.getUTCDate()}, ${date.getUTCFullYear()}`;
};