File size: 861 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { TZDate } from "@date-fns/tz";

/**
 * Parse a date-only string (YYYY-MM-DD) as a UTC calendar date.
 *
 * Use this for date strings from URL params, API responses, or user input
 * where you want to preserve the exact calendar date without timezone shifts.
 *
 * This solves the common problem where:
 * - `parseISO("2026-01-09")` interprets as LOCAL midnight (inconsistent across timezones)
 * - `new Date("2026-01-09T00:00:00Z")` creates UTC midnight, but `format()` shifts it
 *
 * `TZDate(dateStr, "UTC")` maintains timezone context so `format()` respects it.
 *
 * @example
 * // URL param: ?selectedDate=2026-01-09
 * const date = parseDateAsUTC("2026-01-09");
 * format(date, "yyyy-MM-dd"); // Always "2026-01-09" regardless of user's timezone
 */
export function parseDateAsUTC(dateStr: string): TZDate {
  return new TZDate(dateStr, "UTC");
}