Skip to main content
Module

std/datetime/format.ts>format

The Deno Standard Library
Latest
function format
import { format } from "https://deno.land/std@0.223.0/datetime/format.ts";

Formats a date to a string with the specified format.

The following symbols from unicode LDML are supported:

  • yyyy - numeric year
  • yy - 2-digit year
  • M - numeric month
  • MM - 2-digit month
  • d - numeric day
  • dd - 2-digit day
  • H - 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 minute
  • mm - 2-digit minute
  • s - numeric second
  • ss - 2-digit second
  • S - 1-digit fractional second
  • SS - 2-digit fractional second
  • SSS - 3-digit fractional second
  • a - dayPeriod, either AM or PM
  • 'foo' - quoted literal
  • ./- - unquoted literal

Examples

Basic usage

import { format } from "https://deno.land/std@0.223.0/datetime/format.ts";

const date = new Date(2019, 0, 20, 16, 34, 23, 123);

format(date, "dd-MM-yyyy"); // "20-01-2019"

format(date, "MM-dd-yyyy HH:mm:ss.SSS"); // "01-20-2019 16:34:23.123"

format(date, "'today:' yyyy-MM-dd"); // "today: 2019-01-20"

UTC formatting

import { format } from "https://deno.land/std@0.223.0/datetime/format.ts";

const date = new Date(2019, 0, 20, 16, 34, 23, 123);

format(date, "yyyy-MM-dd HH:mm:ss", { utc: true }); // "2019-01-20 05:34:23"

Enable UTC formatting by setting the utc option to true.

Parameters

date: Date

The date to be formatted.

formatString: string

The date time string format.

optional
options: FormatOptions = [UNSUPPORTED]

The options to customize the formatting of the date.

Returns

string

The formatted date string.