All files / util dates.ts

79.41% Statements 27/34
100% Branches 3/3
60% Functions 3/5
79.41% Lines 27/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 351x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x         1x 1x 3x 3x 3x 1x 1x 3x 3x 3x 1x 1x 1x 1x        
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()}`;
};