easydate
Returns the date according to a pattern.
Installation
$ npm install easydate
Test:
$ npm test
Usage/API
easydate(patternString, [config])
The single exported function has two arguments. The first and only required argument is the pattern string (see Pattern Options below). If only including the pattern string it will return a formatted string for the current date-time.
config
(object)
.setDate
(string)
DEFAULT: null
if the optional config object is supplied and includes a date string as the setDate
key value, that particular date will be returned formatted. This input date string must be parseable by JavaScript's Date.parse
function; see below for acceptable examples.
.timeZone
(string: utc
or local
only) BREAKING CHANGE!!!
DEFAULT: local
You can also include a timeZone
key value, for either local
, or utc
to decide how to handle the time zone offset against UTC.
.adjust
(boolean)
DEFAULT: false
Whether or not to adjust DST, see times below.
Times:
-- local --
2016-01-01T00:00:00.000Z --> 00:00:00 UTC+1
2016-02-01T00:00:00.000Z --> 00:00:00 UTC+1
2016-03-01T00:00:00.000Z --> 00:00:00 UTC+1
2016-04-01T00:00:00.000Z --> 01:00:00 UTC+1 DST
2016-05-01T00:00:00.000Z --> 01:00:00 UTC+1 DST
2016-06-01T00:00:00.000Z --> 01:00:00 UTC+1 DST
2016-07-01T00:00:00.000Z --> 01:00:00 UTC+1 DST
2016-08-01T00:00:00.000Z --> 01:00:00 UTC+1 DST
2016-09-01T00:00:00.000Z --> 01:00:00 UTC+1 DST
2016-10-01T00:00:00.000Z --> 01:00:00 UTC+1 DST
2016-11-01T00:00:00.000Z --> 00:00:00 UTC+1
2016-12-01T00:00:00.000Z --> 00:00:00 UTC+1
-- local {adjust: true} --
2016-01-01T00:00:00.000Z --> 00:00:00 UTC+1
2016-02-01T00:00:00.000Z --> 00:00:00 UTC+1
2016-03-01T00:00:00.000Z --> 00:00:00 UTC+1
2016-04-01T00:00:00.000Z --> 00:00:00 UTC+1 DST
2016-05-01T00:00:00.000Z --> 00:00:00 UTC+1 DST
2016-06-01T00:00:00.000Z --> 00:00:00 UTC+1 DST
2016-07-01T00:00:00.000Z --> 00:00:00 UTC+1 DST
2016-08-01T00:00:00.000Z --> 00:00:00 UTC+1 DST
2016-09-01T00:00:00.000Z --> 00:00:00 UTC+1 DST
2016-10-01T00:00:00.000Z --> 00:00:00 UTC+1 DST
2016-11-01T00:00:00.000Z --> 00:00:00 UTC+1
2016-12-01T00:00:00.000Z --> 00:00:00 UTC+1
-- utc --
2016-01-01T00:00:00.000Z --> 00:00:00 UTC
2016-02-01T00:00:00.000Z --> 00:00:00 UTC
2016-03-01T00:00:00.000Z --> 00:00:00 UTC
2016-04-01T00:00:00.000Z --> 00:00:00 UTC DST
2016-05-01T00:00:00.000Z --> 00:00:00 UTC DST
2016-06-01T00:00:00.000Z --> 00:00:00 UTC DST
2016-07-01T00:00:00.000Z --> 00:00:00 UTC DST
2016-08-01T00:00:00.000Z --> 00:00:00 UTC DST
2016-09-01T00:00:00.000Z --> 00:00:00 UTC DST
2016-10-01T00:00:00.000Z --> 00:00:00 UTC DST
2016-11-01T00:00:00.000Z --> 00:00:00 UTC
2016-12-01T00:00:00.000Z --> 00:00:00 UTC
Examples:
var easydate = require('easydate')
easydate('d-M-y')
easydate('d/M/Y')
easydate('Y.M.d')
easydate('M')
easydate('d-M-Y @ h:m:s.l')
easydate('d-M-Y @ h:m', '2015-11-03T16:06:00.000Z')
easydate('h:m:s.l', '2015-11-03T16:06:08.123Z')
easydate('M~d~Y', '03-01-2017')
easydate('d/M/y', {setDate: '2016-10-01T00:00:00.000Z', timeZone: 'utc'})
easydate('d/M/y', {setDate: '2016-10-01T00:00:00.000Z', timeZone: 'local'})
easydate('z', {timeZone: 'utc'})
easydate('z', {timeZone: 'local'})
easydate('h:m:s z x', {setDate: '2016-08-01T00:00:00.000Z'})
easydate('h:m:s z x', {setDate: '2016-08-01T00:00:00.000Z', adjust: true})
Pattern Options
Y
Full year (number - e.g. 2012
)y
Year (number - e.g. 12
)M
Month (number - e.g. 11
)d
Day (number - e.g. 28
)h
Hour (number - e.g. 02
)m
Minute (number - e.g. 01
)s
Second (number - e.g. 33
)l
Millisecond (number - e.g. 001
)z
Timezone (string - e.g. UTC
, UTC+1
, UTC-11
)x
DST (string - either 'DST'
or ''
)
N.B. Case sensitive
Caveats
Any instances of the above characters will be replaced with the relevant numbers. It is recommended to not use words within the pattern string.
License
MIT