Home

Awesome

A simple clean way to format dates with Javascript

Setup

npm install simple-format-date

Usage

import formatDate from 'simple-format-date';

// default output
formatDate(new Date(1973, 0, 2)); // => '1973-01-02'

// using a string as template
formatDate(new Date(1973, 0, 2), { template: '<%= DD %>/<%= MM %>/<%= YY %>' }); // => '02/01/1973' (italian format)

// using a function as template
const months = {
  '01': 'January',
  '02': 'February',
  '03': 'March',
  '04': 'April',
  '05': 'May',
  '06': 'June',
  '07': 'July',
  '08': 'August',
  '09': 'September',
  '10': 'October',
  '11': 'November',
  '12': 'December'
};

formatDate(new Date(1973, 0, 2), {
  template: (locals) => `${locals.DD}, ${months[locals.MM]} ${locals.YY}`
}); // => '02, January 1973'

API

formatDate(date, options)

where: