formatDate function is a versatile tool for converting and formatting date and time strings from various input formats to a customizable output format.ts
import { formatDate } from "@arkyn/shared";
dateTime[string, string?]inputFormat"brazilianDate" | "isoDate" | "timestamp"outputFormatstringtimezonenumber0 (UTC)inputFormat)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.outputFormat)outputFormat string using the following markers:YYYY: Year with 4 digits (e.g., 2023)YY: Year with 2 digits (e.g., 23)MM: Month with 2 digits (01-12)DD: Day with 2 digits (01-31)hh: Hour with 2 digits (00-23)mm: Minute with 2 digits (00-59)ss: Second with 2 digits (00-59)string with the date formatted according to the outputFormat string.Error("Invalid input format"): If the provided inputFormat is not one of the expected values.Error("Invalid date"): If the provided date string cannot be converted to a valid date.javascript
import { formatDate } from "./formatDate";const formatted = formatDate(["25/12/2023", "15:30:00"],"brazilianDate","YYYY-MM-DD hh:mm:ss");console.log(formatted); // Output: "2023-12-25 15:30:00"
javascript
import { formatDate } from "./formatDate";const formatted = formatDate(["2023-12-25", "15:30:00"],"isoDate","DD/MM/YYYY hh:mm:ss",-3 // Adjustment to UTC-3);console.log(formatted);// Output: "2023-12-25 12:30:00"
javascript
import { formatDate } from "./formatDate";const formatted = formatDate(["2024-01-05"], "isoDate", "DD/MM/YYYY");console.log(formatted);// Output: "05/01/2024"