formatDate function is a utility for formatting date and time strings from various input formats to a custom output format, with support for timezone adjustments.ts
import { formatDate } from "@arkyn/shared";
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" format 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"outputFormat (required)YYYY for four-digit year (e.g., 2023),YY for two-digit year (e.g., 23),MM for two-digit month (01-12),DD for two-digit day (01-31),hh for two-digit hour in 24-hour format (00-23),mm for two-digit minute (00-59),ss for two-digit second (00-59). You can combine these placeholders with any text or separators to create the exact format you need.stringtimezone (optional)-3 for UTC-3 (Brasília Time in Brazil), 1 for UTC+1 (Central European Time), or 5 for UTC+5 (Pakistan Standard Time). If not specified, the function uses UTC+0 (Coordinated Universal Time) as the default.number0 (UTC)stringtypescript
import { formatDate } from "@arkyn/shared";const formattedDate = formatDate(["25/12/2023"],"brazilianDate","YYYY-MM-DD hh:mm",);console.log(formattedDate); // "2023-12-25 00:00"
inputFormat parameter is not one of the three allowed values: "brazilianDate", "isoDate", or "timestamp". Make sure you're using the correct format identifier that matches your input date structure."Invalid date" is thrown when the combination of date parts creates an impossible date, such as February 30th or April 31st. The function uses JavaScript's Date object validation to ensure the final date is legitimate.typescript
import { formatDate } from "@arkyn/shared";try {const invalidDate = formatDate(["31/02/2023"], "brazilianDate", "YYYY-MM-DD");console.log(invalidDate);} catch (error) {console.error(error);// Output: Error: Day 31 is not valid for February}
timezone parameter with the appropriate offset value./ (slash) for Brazilian dates or - (hyphen) for ISO and timestamp formats. Additionally, both single-digit and double-digit days and months are accepted, so "5/3/2023" and "05/03/2023" are equally valid inputs.