format
import { format } from "jsr:@ende/datetime";function
function format(date: Date, formatString: string, options: FormatOptions = [UNSUPPORTED]): stringFormats a date to a string with the specified format.
The following symbols from unicode LDML are supported:
yyyy- numeric yearyy- 2-digit yearM- numeric monthMM- 2-digit monthd- numeric daydd- 2-digit dayH- numeric hour (0-23 hours)HH- 2-digit hour (00-23 hours)h- numeric hour (1-12 hours)hh- 2-digit hour (01-12 hours)m- numeric minutemm- 2-digit minutes- numeric secondss- 2-digit secondS- 1-digit fractional secondSS- 2-digit fractional secondSSS- 3-digit fractional seconda- dayPeriod, eitherAMorPM'foo'- quoted literal./-- unquoted literal
date: DateThe date to be formatted.
formatString: stringThe date time string format.
options: FormatOptions = [UNSUPPORTED]The options to customize the formatting of the date.
Returns
The formatted date string.
Example: Basic usage
import { format } from "@ende/datetime/format";
import { assertEquals } from "@ende/assert";
const date = new Date(2019, 0, 20, 16, 34, 23, 123);
assertEquals(format(date, "dd-MM-yyyy"), "20-01-2019");
assertEquals(format(date, "MM-dd-yyyy HH:mm:ss.SSS"), "01-20-2019 16:34:23.123");
assertEquals(format(date, "'today:' yyyy-MM-dd"), "today: 2019-01-20");
Example: UTC formatting
Enable UTC formatting by setting the timeZone option to "UTC".
import { format } from "@ende/datetime/format";
import { assertEquals } from "@ende/assert";
const date = new Date(2019, 0, 20, 16, 34, 23, 123);
assertEquals(format(date, "yyyy-MM-dd HH:mm:ss", { timeZone: "UTC" }), "2019-01-20 05:34:23");