0.2.11 • Published 8 months ago
dtfm v0.2.11
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"
0.2.11
8 months ago
0.2.10
8 months ago
0.2.7
2 years ago
0.2.9
2 years ago
0.2.8
2 years ago
0.2.6
2 years ago
0.2.5
2 years ago
0.2.4
2 years ago
0.2.3
2 years ago
0.2.2
2 years ago
0.2.1
2 years ago
0.2.0
2 years ago
0.1.3
2 years ago
0.1.2
2 years ago
0.1.1
2 years ago
0.1.0
2 years ago
0.0.10
2 years ago
0.0.9
2 years ago
0.0.8
2 years ago
0.0.7
2 years ago
0.0.6
2 years ago
0.0.5
2 years ago
0.0.4
2 years ago
0.0.3
2 years ago
0.0.2
2 years ago
0.0.1
2 years ago