parseToDate function is a utility for converting date and time strings from various input formats into JavaScript Date objects with timezone adjustment support. This function is particularly useful when working with dates from different sources or locales that need to be standardized into Date objects.ts
import { parseToDate } from "@arkyn/shared/parseToDate";
dateTime (required)[string, string?]inputFormat (required)"brazilianDate" expects dates in "DD/MM/YYYY" or "D/M/YYYY" format (e.g., "25/12/2023" or "5/3/2023"), commonly used in Brazil and other countries."isoDate" expects dates in "MM-DD-YYYY" or "M-D-YYYY" format (e.g., "12-25-2023" or "3-5-2023"), following the American date convention."timestamp" expects dates in "YYYY-MM-DD" or "YYYY-M-D" format (e.g., "2023-12-25" or "2023-3-5"), which is the ISO 8601 standard format."brazilianDate" | "isoDate" | "timestamp"timezone (optional)number0 (UTC)Date object representing the parsed date and time, adjusted for the specified timezone. The returned Date object is in UTC but reflects the timezone adjustment applied.Datetypescript
import { parseToDate } from "@arkyn/shared/parseToDate";const date = parseToDate(["25/12/2023", "15:30:00"], "brazilianDate", -3);console.log(date);// Output: 2023-12-25T12:30:00.000Z (UTC, adjusted for -3 timezone)
inputFormat value is provided. Only "brazilianDate", "isoDate", and "timestamp" are valid.Date object cannot be constructed, which happens specifically when the (unvalidated) time portion of the input contains non-numeric or otherwise invalid values, such as "AB:CD:EF". Note that malformed or non-numeric date parts are caught earlier by ValidateDateService, which throws a more specific error first (e.g., "Year should be four digits" for a year that isn't a 4-digit number).timezone parameter (e.g., -3 for UTC-3 or +5 for UTC+5).ValidateDateService to ensure the input format and date components are valid before creating the Date object.