intl-dateformat is a simple date formatting library that leverage the Intl.DateTimeFormat API to format dates in different languages and timezones without having to clutter your JavaScript bundles.
Features
✌ Small: As it directly leverages Intl.DateTimeFormat, there is no need to bundle additional locales or a timezones database. It's already in your Browser!
👌Simple: It supports a subset of ISO 8601 formats, discarding very rarely used date parts.
🤟 Extensible: That said, if you want to customize things you can pass custom formatters.
Installation
$ npm install intl-dateformat
Example
import formatDate from 'intl-dateformat'const date = new Date(Date.UTC(1984, 0, 17, 16, 13, 37, 0))formatDate(date, 'YYYY-MM-DD hh:mm:ss A')// → 1984-01-17 04:13:37 PMformatDate(date, 'YYYY-MM-DD hh:mm:ss A', { timezone: 'Asia/Singapore' })// → 1984-01-18 00:13:37 AMformatDate(date, 'YYYY, MMMM dddd DD')// → 1984, January Tuesday 17formatDate(date, '[It is] dddd [today!]')// → It is Tuesday today!formatDate(date, 'YYYY, MMMM dddd DD', { locale: 'fr' })
Usage
import formatDate from 'intl-dateformat'const date = new Date(Date.UTC(1984, 0, 17, 16, 13, 37, 0))formatDate(date, 'YYYY-MM-DD hh:mm:ss A')// → 1984-01-17 04:13:37 PM
If you find yourself missing some date parts, no problem we got you covered. You can create your own dateFormat function and add your custom formatters:
Formatters are represented as a dictionary of functions, where the key represents the mask that is to be matched in the format and the value is the function that will format the date.
The formatter function takes the following arguments:
parts - An object containing all the date parts provided by Intl.DateTimeFormat. You can inspect the DatePartName type for an exhaustive list of all the date parts
date - The original date passed to the formatDate function.