formatToDate function converts a date string (with optional time) from various formats to a JavaScript Date object, applying a time zone adjustment.ts
import { formatToDate } from "@arkyn/shared";
dateTime[string, string?]inputFormattimezone (optional)number0 (UTC).0inputFormat)brazilianDate: Expects the date in DD/MM/YYYY format.isoDate: Expects the date in YYYY-MM-DD format.timestamp: Expects the date in YYYY-MM-DD format.Date object representing the parsed date and time, adjusted for the specified time zone.Error("Invalid input format"): If the provided inputFormat is invalid.Error("Invalid date"): If the provided date or time is invalid.javascript
import { formatToDate } from "./formatToDate";// Converts the date to a Date object, considering the UTC-3 time zoneconst date = formatToDate(["25/12/2023", "15:30:00"], "brazilianDate", -3);console.log(date.toISOString());// Output: "2023-12-25T18:30:00.000Z"
javascript
import { formatToDate } from "./formatToDate";const date = formatToDate(["2023-12-25"], "isoDate");console.log(date.toISOString());// Output: "2023-12-25T00:00:00.000Z"