Simple time/date formatter for JavaScript which uses PHP-like syntax to easily format JavaScript date objects into legible/formatted strings. It works in the browser and Node.js and is quite small ~1.3KB (minified & gzipped).
First, install the module via NPM:
npm i timeUtils
1
Then include the module...
As an import:
import { formatDate } from 'timeUtils'
As a Common JS library:
const formatDate = require('timeUtils').formatDate;
As a script tag:
<script type="text/javascript" src="node_modules/timeUtils/dist/timeUtils.umd.js" />
timeUtils's formatDate
function expects a JavaScript Date object as its first argument and a string-based template as its second argument.
The templates should take the following form:
"Lorem Ipsum '#{m}/#{d}/#{Y}'"
Where strings wrapped in #{}
represent date/time variables which will be formatted for you. The table below shows all the variables currently available along with a brief description. In the above example template, we would get date in the "mm/dd/yyyy" format - for example.
Symbol/Token | Type | Description | Example(s) |
---|---|---|---|
D | day of week | Textual representation of day of week, 3 letters | Sun, Mon, ... Sat |
l | day of week | Full textual representation of day of week | Sunday, Monday, ... Saturday |
F | month | Full textual representation of the month | January, ... December |
M | month | Abbreviated textual representation of month, 3 letters | Jan, Feb, ... Dec |
n | month | Numeric representation of month without leading 0's | 1, 2, ... 12 |
m | month | Numeric representation of the month with leading 0's | 01, 02, ... 12 |
j | day of month | Day of month, without leading 0's | 1, 2, ... 31 |
d | day of month | Day of the month, 2 digits, with leading zeros | 01, 02, ... 31 |
y | year | Short numeric year, 2 digits | 00, 01, ... 15 |
Y | year | Full numeric year, 4 digits | 2000, 2001, ... 2015 |
Symbol/Token | Type | Description | Example(s) |
---|---|---|---|
g | hour | 12-hour format of the hour, without leading 0's | 1, 2, ... 12 |
h | hour | 12-hour format of the hour, with leading 0's | 01, 02, ... 12 |
G | hour | 24-hour format of the hour, without leading 0's | 0, 1, ... 23 |
H | hour | 24-hour format of the hour, with leading 0's | 00, 01, ... 23 |
i | minute | Minutes with leading 0's | 00, 01, ... 59 |
s | seconds | Seconds with leading 0's | 00, 01, ... 59 |
a | am/pm | Lowercase ante meridiem and post meridiem | 'am' or 'pm' |
A | am/pm | Uppercase ante meridiem and post meridiem | 'AM' or 'PM' |
timeUtils provides an internationalize
method which can be used to support multiple languages:
import { internationalize } from 'timeUtils';
internationalize({
daysOfWeek: [
'Domingo',
'Lunes',
'Martes',
'Miércoles',
'Jueves',
'Viernes',
'Sábado'
],
monthsOfYear: [
'Enero',
'Febrero',
'Marzo',
'Abril',
'Mayo',
'Junio',
'Julio',
'Agosto',
'Septiembre',
'Octubre',
'Noviembre',
'Diciembre'
]
})
var date = new Date()
Mon Apr 06 2015 21:51:08 GMT-0600 (Mountain Daylight Time)
timeUtils.formatDate(date,'Your appointment is on #{l}, #{F} #{j}, #{Y} at #{g}:#{i} #{A}.')
"Your appointment is on Monday, April 6, 2015 at 9:48 PM."