dtfm

0.2.11 • Public • Published

dtfm

Locale-agnostic date formatting utilities

Date formatting

formatDate(new Date(), '{YYYY}-{MM}-{DD} {HH}:{mm}:{ss}');
// = 2022-07-19 12:34:56

List of placeholders

Placeholder Value
{Y} Year
{YY} Year, zero-prefixed when shorter than 2 digits
{YYYY} Year, zero-prefixed when shorter than 4 digits
{yy} Year, last 2 digits zero-prefixed when shorter than 2 digits
{YE} Year of a calendar era (specified by AD/BC or CE/BCE)
{E} Christian era: AD/BC
{CE} Common Era: CE/BCE
{M} Month
{MM} Month, zero-prefixed when shorter than 2 digits
{MMM} Month abbreviation
{D} Day
{DD} Day, zero-prefixed when shorter than 2 digits
{H} Hours
{HH} Hours, zero-prefixed when shorter than 2 digits
{h} 12h-clock hours
{hh} 12h-clock hours, zero-prefixed when shorter than 2 digits
{a} Day period: AM/PM
{m} Minutes
{mm} Minutes, zero-prefixed when shorter than 2 digits
{s} Seconds
{ss} Seconds, zero-prefixed when shorter than 2 digits
{ms} Milliseconds, zero-prefixed when shorter than 3 digits
{tz} Time zone
{WD} Week day abbreviation
{iso} Local time zone ISO date string
{isoDate} Date (= {YYYY}-{MM}-{DD})
{isoTime} Time (= {HH}:{mm}:{ss})
{isoTimeMs} Time with milliseconds

The following unformatted date components are also available as placeholder values: timestamp, year, month, day, hours, hours12 (12h-clock hours), minutes, seconds, milliseconds, weekDay, timezoneOffset, input (the first argument of the formatDate() call).

Using external locale-specific formats

formatDate(new Date(), customLocale.fullDate);
// = þriðjudagur 19. júlí 2022
// custom-locale.js (outside the package)
const customWeekDays = [
    'sunnudagur',
    'mánudagur',
    'þriðjudagur',
    'miðvikudagur',
    'fimmtudagur',
    'föstudagur',
    'laugardagur',
];
const customMonths = [
    'janúar',
    'febrúar',
    'mars',
    'apríl',
    'maí',
    'júní',
    'júlí',
    'ágúst',
    'september',
    'október',
    'nóvember',
    'desember',
];
const customFullDateFormat = {
    template: '{WD} {D}. {MMM} {Y}',
    transform: {
        WD: ({weekDay}) => customWeekDays[weekDay],
        MMM: ({month}) => customMonths[month - 1],
    },
};

export const customLocale = {
    fullDate: customFullDateFormat,
};

Dynamic templates

let eraTemplate = ({E}) => E === 'AD' ? '{E} {YE}' : '{YE} {E}';

formatDate('2022-07-19', eraTemplate);
// = AD 2022

formatDate(-62200000000000, eraTemplate);
// = 3 BC

Durations

formatDuration(123456);
// = 2'03.456"

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.2.1114latest

Version History

VersionDownloads (Last 7 Days)Published
0.2.1114
0.2.102
0.2.90
0.2.80
0.2.70
0.2.60
0.2.50
0.2.40
0.2.30
0.2.20
0.2.10
0.2.00
0.1.30
0.1.20
0.1.10
0.1.00
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i dtfm

Weekly Downloads

16

Version

0.2.11

License

ISC

Unpacked Size

25.8 kB

Total Files

18

Last publish

Collaborators

  • axtk