0.3.0 • Published 9 years ago
simple-format-date v0.3.0
A simple clean way to format dates with Javascript
Setup
npm install simple-format-dateUsage
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:
datethe date to formatoptionstemplate: string | (locals: Object) => stringthe format to use in ejs syntax (default<%= YY %>-<%= MM %>-<%= DD %>) wherelocalsis an object containing the following keys:Yshort year ('15'for 2015)YYnumeric long year (2015)Mnumeric month (9for September)MMpadded month ('09'for September)Dnumeric dayDDpadded dayhnumeric hourshhpadded hoursmnumeric minutesmmpadded minutessnumeric secondssspadded seconds or a function returning a template