Easy way to format dates in JavaScript
Easy way to format dates in JavaScript
Advertisement

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 PM
formatDate(date, 'YYYY, MMMM dddd DD')
// → 1984, January Tuesday 17
formatDate(date, 'YYYY-MM-DD hh:mm:ss A', { timezone: 'Asia/Singapore' })
// → 1984-01-18 00:13:37 AM
formatDate(date, 'YYYY, MMMM dddd DD', { locale: 'fr' })
// → 1984, Janvier Mercredi 18

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

You can view the full documentation by navigating to this link

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.