All files / lib/packages/shared-utils date-helper.ts

100% Statements 24/24
92.85% Branches 13/14
60% Functions 6/10
100% Lines 21/21

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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50      215x 46x 46x 46x       48x 2x     46x 46x   46x 46x       27x   27x     215x         40x   40x 39x 39x   1x     215x       2x   2x    
import { TZDate } from "@date-fns/tz";
import { add, format } from "date-fns";
 
export const isDST = (date: Date): boolean => {
  const jan = new Date(date).getTimezoneOffset();
  const jul = new Date(new Date(date).setMonth(6)).getTimezoneOffset();
  return new Date(date).getTimezoneOffset() < Math.max(jan, jul);
};
 
export function formatNinetyDaysDate(date: number | null | undefined): string {
  if (!date) {
    return "Pending";
  }
 
  const baseDate = new Date(date);
  const ninetyDaysLater = add(baseDate, { days: 90 });
 
  const timezoneAbbreviation = isDST(ninetyDaysLater) ? "EDT" : "EST";
  return format(ninetyDaysLater, `MMM d, yyyy '@ 11:59pm ${timezoneAbbreviation}'`);
}
 
export function formatDate(dateValue: string | number) {
  const dateObj = new Date(dateValue);
 
  return format(dateObj, "MMMM d, yyyy");
}
 
export const formatDateToET = (
  utcDateValue: string | number,
  formatValue: string = "eee, MMM d yyyy, hh:mm:ss a",
  includeTimezone: boolean = true,
) => {
  const utcDateObj = new TZDate(new Date(utcDateValue).toISOString(), "America/New_York");
 
  if (includeTimezone) {
    const tzTag = format(utcDateObj, "z") === "GMT-5" ? "EST" : "EDT";
    return format(utcDateObj, `${formatValue} '${tzTag}'`);
  }
  return format(utcDateObj, formatValue);
};
 
export const formatDateToUTC = (
  utcDateValue: string | number,
  formatValue: string = "MMMM d, yyyy",
) => {
  const utcDateObj = new TZDate(new Date(utcDateValue).toISOString(), "UTC");
 
  return format(utcDateObj, formatValue);
};