easydate
Returns the date according to a pattern.
Installation
$ npm install easydate
...or:
$ yarn add easydate
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 = // current date/time // "28-01-14" // "28/01/2014" // "2014.01.28" // "01" // "29-01-2014 @ 07:22:37.418" // specified date/time // "03-11-2015 @ 16:06" // "16:06:08.123" // "03~01~2017" // time zone (e.g. in UTC+1) // => "30/09/16" // => "01/10/16" // => "UTC" // => "UTC+1" // => "01:00:00 UTC+1 DST" // => "00:00:00 UTC+1 DST"
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