Date Utilities API reference for date manipulation and formatting functions calendar API Reference
Categories

Date Utilities

The Date utilities provide functions for formatting and manipulating dates in JavaScript.

Functions

formatDate

function formatDate(date, format = 'LLL', options = {})

Formats a date according to the specified format string and options.

International Support This function uses Intl.DateTimeFormat internally, please refer to the associated docs for details on timezone and formatting usage. Formatter instances are cached internally for performance, so repeated calls with the same locale and timezone avoid re-creating Intl.DateTimeFormat objects.

Timezone Lookup You can use IANA.org’s guide to look up supported timezones.

Local Timezone Usage When the ‘local’ keyword is used for the timezone option, the function will use the local timezone of the environment where the code is running. This is particularly useful for displaying dates in the user’s local time without needing to detect the timezone programmatically.

Timezone Shorthand

formatDate provides additional shorthand timezones that can be used instead of their city counterparts.

const shorthand = reverseKeys({
// North America
'America/New_York': ['ET'],
'America/Chicago': ['CT'],
'America/Denver': ['MT'],
'America/Los_Angeles': ['PT'],
'America/Anchorage': ['AKT'],
'Pacific/Honolulu': ['HT'],
'America/Halifax': ['AT'],
// South America
'America/Sao_Paulo': ['BRT'],
// Europe
'Europe/London': ['UK', 'WET'],
'Europe/Paris': ['CET', 'ECT'],
'Europe/Helsinki': ['EET'],
'Europe/Dublin': ['IRST'], // Note disambiguation with India
// Australia/Oceania
'Australia/Sydney': ['AET'],
'Australia/Adelaide': ['ACT'],
'Australia/Perth': ['AWT'],
'Pacific/Auckland': ['NZT'],
// Asia
'Asia/Kolkata': ['IST', 'INST'], // Note disambiguation with Ireland
'Asia/Tokyo': ['JST'],
'Asia/Singapore': ['SGT']
});

Parameters

NameTypeDefaultDescription
dateDateThe date to format
formatstring’LLL’The format string (e.g., ‘YYYY-MM-DD’, ‘LT’, ‘LL’)
optionsobjectAdditional formatting options
Options
NameTypeDefaultDescription
localestring’default’The locale to use for formatting
hour12booleantrueWhether to use 12-hour time. When false, the hh and h tokens output 24-hour values and the a token outputs an empty string.
timezonestring’UTC’The timezone to use. Use 'local' for the browser’s local timezone.

Returns

A formatted date string, or 'Invalid Date' if the input is not a valid date.

Format Tokens

TokenOutputExample
YYYY4-digit year2023
YY2-digit year23
MMMMFull month nameJanuary
MMM3-letter monthJan
MMZero-padded month01
MMonth number1
DDZero-padded day05
DDay number5
DoDay with ordinal suffix5th
ddddFull weekday nameThursday
ddd3-letter weekdayThu
HH24-hour padded14
hh12-hour padded (or 24-hour when hour12: false)02
h12-hour (or 24-hour when hour12: false)2
mmZero-padded minutes05
ssZero-padded seconds09
aam/pm (empty when hour12: false)pm

Text inside square brackets is treated as a literal and passed through without replacement: [Today is] dddd outputs Today is Thursday.

Preset Formats

PresetPatternExample
LTh:mm a3:34 pm
LTSh:mm:ss a3:34:56 pm
LMM/DD/YYYY05/18/2023
lM/D/YYYY5/18/2023
LLMMMM D, YYYYMay 18, 2023
llMMM D, YYYYMay 18, 2023
LLLMMMM D, YYYY h:mm aMay 18, 2023 3:34 pm
lllMMM D, YYYY h:mm aMay 18, 2023 3:34 pm
LLLLdddd, MMMM D, YYYY h:mm aThursday, May 18, 2023 3:34 pm
llllddd, MMM D, YYYY h:mm aThu, May 18, 2023 3:34 pm

Example

import { formatDate } from '@semantic-ui/utils';
const date = new Date('2023-05-15T14:30:00Z');
console.log(formatDate(date)); // "May 15, 2023 2:30 pm"
console.log(formatDate(date, 'YYYY-MM-DD')); // "2023-05-15"
console.log(formatDate(date, 'LT', { timezone: 'America/New_York' })); // "10:30 am"
console.log(formatDate(date, 'LT', { timezone: 'ET' })); // "10:30 am" (shorthand)
console.log(formatDate(date, 'LT', { timezone: 'local' })); // Formats using the local timezone
// 24-hour format — hh and h give 24h values, a gives empty string
console.log(formatDate(date, 'HH:mm', { hour12: false })); // "14:30"
console.log(formatDate(date, 'hh:mm a', { hour12: false })); // "14:30 "
// Escaped literal text
console.log(formatDate(date, '[Today is] dddd, MMMM Do, YYYY')); // "Today is Monday, May 15th, 2023"
Previous
Crypto
Next
Debug