1 | date-format
|
---|
2 | ===========
|
---|
3 |
|
---|
4 | node.js formatting of Date objects as strings. Probably exactly the same as some other library out there.
|
---|
5 |
|
---|
6 | ```sh
|
---|
7 | npm install date-format
|
---|
8 | ```
|
---|
9 |
|
---|
10 | usage
|
---|
11 | =====
|
---|
12 |
|
---|
13 | Formatting dates as strings
|
---|
14 | ----
|
---|
15 |
|
---|
16 | ```javascript
|
---|
17 | var format = require('date-format');
|
---|
18 | format.asString(); //defaults to ISO8601 format and current date.
|
---|
19 | format.asString(new Date()); //defaults to ISO8601 format
|
---|
20 | format.asString('hh:mm:ss.SSS', new Date()); //just the time
|
---|
21 | ```
|
---|
22 |
|
---|
23 | or
|
---|
24 |
|
---|
25 | ```javascript
|
---|
26 | var format = require('date-format');
|
---|
27 | format(); //defaults to ISO8601 format and current date.
|
---|
28 | format(new Date());
|
---|
29 | format('hh:mm:ss.SSS', new Date());
|
---|
30 | ```
|
---|
31 |
|
---|
32 | Format string can be anything, but the following letters will be replaced (and leading zeroes added if necessary):
|
---|
33 | * dd - `date.getDate()`
|
---|
34 | * MM - `date.getMonth() + 1`
|
---|
35 | * yy - `date.getFullYear().toString().substring(2, 4)`
|
---|
36 | * yyyy - `date.getFullYear()`
|
---|
37 | * hh - `date.getHours()`
|
---|
38 | * mm - `date.getMinutes()`
|
---|
39 | * ss - `date.getSeconds()`
|
---|
40 | * SSS - `date.getMilliseconds()`
|
---|
41 | * O - timezone offset in +hm format (note that time will be in UTC if displaying offset)
|
---|
42 |
|
---|
43 | Built-in formats:
|
---|
44 | * `format.ISO8601_FORMAT` - `2017-03-14T14:10:20.391` (local time used)
|
---|
45 | * `format.ISO8601_WITH_TZ_OFFSET_FORMAT` - `2017-03-14T03:10:20.391+1100` (UTC + TZ used)
|
---|
46 | * `format.DATETIME_FORMAT` - `14 03 2017 14:10:20.391` (local time used)
|
---|
47 | * `format.ABSOLUTETIME_FORMAT` - `14:10:20.391` (local time used)
|
---|
48 |
|
---|
49 | Parsing strings as dates
|
---|
50 | ----
|
---|
51 | The date format library has limited ability to parse strings into dates. It can convert strings created using date format patterns (as above), but if you're looking for anything more sophisticated than that you should probably look for a better library ([momentjs](https://momentjs.com) does pretty much everything).
|
---|
52 |
|
---|
53 | ```javascript
|
---|
54 | var format = require('date-format');
|
---|
55 | // pass in the format of the string as first argument
|
---|
56 | format.parse(format.ISO8601_FORMAT, '2017-03-14T14:10:20.391');
|
---|
57 | // returns Date
|
---|
58 | ```
|
---|