date-format 1.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc +4 -20
- package/.travis.yml +2 -3
- package/README.md +22 -4
- package/lib/index.js +153 -35
- package/package.json +5 -7
- package/test/date_format-test.js +45 -41
- package/test/parse-test.js +177 -0
- package/.npmignore +0 -15
package/.eslintrc
CHANGED
@@ -1,28 +1,12 @@
|
|
1
1
|
{
|
2
|
-
"
|
3
|
-
|
2
|
+
"extends": [
|
3
|
+
"eslint:recommended"
|
4
|
+
],
|
4
5
|
"env": {
|
5
6
|
"node": true,
|
6
7
|
"mocha": true
|
7
8
|
},
|
8
9
|
"plugins": [
|
9
10
|
"mocha"
|
10
|
-
]
|
11
|
-
"rules": {
|
12
|
-
"comma-dangle": 0,
|
13
|
-
"indent": 2,
|
14
|
-
"object-shorthand": 0,
|
15
|
-
"func-names": 0,
|
16
|
-
"max-len": [1, 120, 2],
|
17
|
-
"no-use-before-define": 1,
|
18
|
-
"no-param-reassign": 0,
|
19
|
-
"strict": 0,
|
20
|
-
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["test/*.js"]}],
|
21
|
-
"prefer-spread": 0,
|
22
|
-
"prefer-rest-params": 0,
|
23
|
-
"mocha/no-exclusive-tests": "error"
|
24
|
-
},
|
25
|
-
"parser-options": {
|
26
|
-
"ecmaVersion": 6
|
27
|
-
}
|
11
|
+
]
|
28
12
|
}
|
package/.travis.yml
CHANGED
package/README.md
CHANGED
@@ -10,7 +10,10 @@ npm install date-format
|
|
10
10
|
usage
|
11
11
|
=====
|
12
12
|
|
13
|
-
|
13
|
+
Formatting dates as strings
|
14
|
+
----
|
15
|
+
|
16
|
+
```javascript
|
14
17
|
var format = require('date-format');
|
15
18
|
format.asString(); //defaults to ISO8601 format and current date.
|
16
19
|
format.asString(new Date()); //defaults to ISO8601 format
|
@@ -19,7 +22,7 @@ format.asString('hh:mm:ss.SSS', new Date()); //just the time
|
|
19
22
|
|
20
23
|
or
|
21
24
|
|
22
|
-
```
|
25
|
+
```javascript
|
23
26
|
var format = require('date-format');
|
24
27
|
format(); //defaults to ISO8601 format and current date.
|
25
28
|
format(new Date());
|
@@ -35,6 +38,21 @@ Format string can be anything, but the following letters will be replaced (and l
|
|
35
38
|
* mm - `date.getMinutes()`
|
36
39
|
* ss - `date.getSeconds()`
|
37
40
|
* SSS - `date.getMilliseconds()`
|
38
|
-
* O - timezone offset in +hm format
|
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).
|
39
52
|
|
40
|
-
|
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
|
+
```
|
package/lib/index.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
2
|
|
3
3
|
function padWithZeros(vNumber, width) {
|
4
|
-
|
4
|
+
var numAsString = vNumber.toString();
|
5
5
|
while (numAsString.length < width) {
|
6
|
-
numAsString =
|
6
|
+
numAsString = "0" + numAsString;
|
7
7
|
}
|
8
8
|
return numAsString;
|
9
9
|
}
|
@@ -18,46 +18,47 @@ function addZero(vNumber) {
|
|
18
18
|
* @private
|
19
19
|
*/
|
20
20
|
function offset(timezoneOffset) {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
let m = String(os % 60);
|
21
|
+
var os = Math.abs(timezoneOffset);
|
22
|
+
var h = String(Math.floor(os / 60));
|
23
|
+
var m = String(os % 60);
|
25
24
|
if (h.length === 1) {
|
26
|
-
h =
|
25
|
+
h = "0" + h;
|
27
26
|
}
|
28
27
|
if (m.length === 1) {
|
29
|
-
m =
|
28
|
+
m = "0" + m;
|
30
29
|
}
|
31
|
-
return timezoneOffset < 0 ?
|
30
|
+
return timezoneOffset < 0 ? "+" + h + m : "-" + h + m;
|
32
31
|
}
|
33
32
|
|
34
|
-
function
|
35
|
-
|
36
|
-
|
33
|
+
function datePart(date, displayUTC, part) {
|
34
|
+
return displayUTC ? date["getUTC" + part]() : date["get" + part]();
|
35
|
+
}
|
36
|
+
|
37
|
+
function asString(format, date) {
|
38
|
+
if (typeof format !== "string") {
|
37
39
|
date = format;
|
38
40
|
format = module.exports.ISO8601_FORMAT;
|
39
41
|
}
|
40
42
|
if (!date) {
|
41
|
-
date =
|
42
|
-
}
|
43
|
-
// make the date independent of the system timezone by working with UTC
|
44
|
-
if (timezoneOffset === undefined) {
|
45
|
-
timezoneOffset = date.getTimezoneOffset();
|
43
|
+
date = module.exports.now();
|
46
44
|
}
|
47
45
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
46
|
+
var displayUTC = format.indexOf("O") > -1;
|
47
|
+
|
48
|
+
var vDay = addZero(datePart(date, displayUTC, "Date"));
|
49
|
+
var vMonth = addZero(datePart(date, displayUTC, "Month") + 1);
|
50
|
+
var vYearLong = addZero(datePart(date, displayUTC, "FullYear"));
|
51
|
+
var vYearShort = addZero(vYearLong.substring(2, 4));
|
52
|
+
var vYear = format.indexOf("yyyy") > -1 ? vYearLong : vYearShort;
|
53
|
+
var vHour = addZero(datePart(date, displayUTC, "Hours"));
|
54
|
+
var vMinute = addZero(datePart(date, displayUTC, "Minutes"));
|
55
|
+
var vSecond = addZero(datePart(date, displayUTC, "Seconds"));
|
56
|
+
var vMillisecond = padWithZeros(
|
57
|
+
datePart(date, displayUTC, "Milliseconds"),
|
58
|
+
3
|
59
|
+
);
|
60
|
+
var vTimeZone = offset(date.getTimezoneOffset());
|
61
|
+
var formatted = format
|
61
62
|
.replace(/dd/g, vDay)
|
62
63
|
.replace(/MM/g, vMonth)
|
63
64
|
.replace(/y{1,4}/g, vYear)
|
@@ -69,9 +70,126 @@ function asString(format, date, timezoneOffset) {
|
|
69
70
|
return formatted;
|
70
71
|
}
|
71
72
|
|
73
|
+
function extractDateParts(pattern, str, missingValuesDate) {
|
74
|
+
var matchers = [
|
75
|
+
{
|
76
|
+
pattern: /y{1,4}/,
|
77
|
+
regexp: "\\d{1,4}",
|
78
|
+
fn: function(date, value) {
|
79
|
+
date.setFullYear(value);
|
80
|
+
}
|
81
|
+
},
|
82
|
+
{
|
83
|
+
pattern: /MM/,
|
84
|
+
regexp: "\\d{1,2}",
|
85
|
+
fn: function(date, value) {
|
86
|
+
date.setMonth(value - 1);
|
87
|
+
}
|
88
|
+
},
|
89
|
+
{
|
90
|
+
pattern: /dd/,
|
91
|
+
regexp: "\\d{1,2}",
|
92
|
+
fn: function(date, value) {
|
93
|
+
date.setDate(value);
|
94
|
+
}
|
95
|
+
},
|
96
|
+
{
|
97
|
+
pattern: /hh/,
|
98
|
+
regexp: "\\d{1,2}",
|
99
|
+
fn: function(date, value) {
|
100
|
+
date.setHours(value);
|
101
|
+
}
|
102
|
+
},
|
103
|
+
{
|
104
|
+
pattern: /mm/,
|
105
|
+
regexp: "\\d\\d",
|
106
|
+
fn: function(date, value) {
|
107
|
+
date.setMinutes(value);
|
108
|
+
}
|
109
|
+
},
|
110
|
+
{
|
111
|
+
pattern: /ss/,
|
112
|
+
regexp: "\\d\\d",
|
113
|
+
fn: function(date, value) {
|
114
|
+
date.setSeconds(value);
|
115
|
+
}
|
116
|
+
},
|
117
|
+
{
|
118
|
+
pattern: /SSS/,
|
119
|
+
regexp: "\\d\\d\\d",
|
120
|
+
fn: function(date, value) {
|
121
|
+
date.setMilliseconds(value);
|
122
|
+
}
|
123
|
+
},
|
124
|
+
{
|
125
|
+
pattern: /O/,
|
126
|
+
regexp: "[+-]\\d{3,4}|Z",
|
127
|
+
fn: function(date, value) {
|
128
|
+
if (value === "Z") {
|
129
|
+
value = 0;
|
130
|
+
}
|
131
|
+
var offset = Math.abs(value);
|
132
|
+
var minutes = (offset % 100) + Math.floor(offset / 100) * 60;
|
133
|
+
date.setMinutes(date.getMinutes() + (value > 0 ? minutes : -minutes));
|
134
|
+
}
|
135
|
+
}
|
136
|
+
];
|
137
|
+
|
138
|
+
var parsedPattern = matchers.reduce(
|
139
|
+
function(p, m) {
|
140
|
+
if (m.pattern.test(p.regexp)) {
|
141
|
+
m.index = p.regexp.match(m.pattern).index;
|
142
|
+
p.regexp = p.regexp.replace(m.pattern, "(" + m.regexp + ")");
|
143
|
+
} else {
|
144
|
+
m.index = -1;
|
145
|
+
}
|
146
|
+
return p;
|
147
|
+
},
|
148
|
+
{ regexp: pattern, index: [] }
|
149
|
+
);
|
150
|
+
|
151
|
+
var dateFns = matchers.filter(function(m) {
|
152
|
+
return m.index > -1;
|
153
|
+
});
|
154
|
+
dateFns.sort(function(a, b) {
|
155
|
+
return a.index - b.index;
|
156
|
+
});
|
157
|
+
|
158
|
+
var matcher = new RegExp(parsedPattern.regexp);
|
159
|
+
var matches = matcher.exec(str);
|
160
|
+
if (matches) {
|
161
|
+
var date = missingValuesDate || module.exports.now();
|
162
|
+
dateFns.forEach(function(f, i) {
|
163
|
+
f.fn(date, matches[i + 1]);
|
164
|
+
});
|
165
|
+
return date;
|
166
|
+
}
|
167
|
+
|
168
|
+
throw new Error(
|
169
|
+
"String '" + str + "' could not be parsed as '" + pattern + "'"
|
170
|
+
);
|
171
|
+
}
|
172
|
+
|
173
|
+
function parse(pattern, str, missingValuesDate) {
|
174
|
+
if (!pattern) {
|
175
|
+
throw new Error("pattern must be supplied");
|
176
|
+
}
|
177
|
+
|
178
|
+
return extractDateParts(pattern, str, missingValuesDate);
|
179
|
+
}
|
180
|
+
|
181
|
+
/**
|
182
|
+
* Used for testing - replace this function with a fixed date.
|
183
|
+
*/
|
184
|
+
function now() {
|
185
|
+
return new Date();
|
186
|
+
}
|
187
|
+
|
72
188
|
module.exports = asString;
|
73
189
|
module.exports.asString = asString;
|
74
|
-
module.exports.
|
75
|
-
module.exports.
|
76
|
-
module.exports.
|
77
|
-
module.exports.
|
190
|
+
module.exports.parse = parse;
|
191
|
+
module.exports.now = now;
|
192
|
+
module.exports.ISO8601_FORMAT = "yyyy-MM-ddThh:mm:ss.SSS";
|
193
|
+
module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = "yyyy-MM-ddThh:mm:ss.SSSO";
|
194
|
+
module.exports.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS";
|
195
|
+
module.exports.ABSOLUTETIME_FORMAT = "hh:mm:ss.SSS";
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "date-format",
|
3
|
-
"version": "1.0
|
3
|
+
"version": "2.1.0",
|
4
4
|
"description": "Formatting Date objects as strings since 2013",
|
5
5
|
"main": "lib/index.js",
|
6
6
|
"repository": {
|
@@ -25,11 +25,9 @@
|
|
25
25
|
"readmeFilename": "README.md",
|
26
26
|
"gitHead": "bf59015ab6c9e86454b179374f29debbdb403522",
|
27
27
|
"devDependencies": {
|
28
|
-
"eslint": "^
|
29
|
-
"eslint-
|
30
|
-
"
|
31
|
-
"
|
32
|
-
"mocha": "^3.2.0",
|
33
|
-
"should": "^11.1.2"
|
28
|
+
"eslint": "^5.16.0",
|
29
|
+
"eslint-plugin-mocha": "^5.3.0",
|
30
|
+
"mocha": "^5.2.0",
|
31
|
+
"should": "^13.2.3"
|
34
32
|
}
|
35
33
|
}
|
package/test/date_format-test.js
CHANGED
@@ -1,60 +1,64 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
3
|
require('should');
|
4
|
-
|
4
|
+
|
5
|
+
var dateFormat = require('../lib');
|
5
6
|
|
6
7
|
function createFixedDate() {
|
7
|
-
|
8
|
+
return new Date(2010, 0, 11, 14, 31, 30, 5);
|
8
9
|
}
|
9
10
|
|
10
|
-
describe('date_format', ()
|
11
|
-
|
11
|
+
describe('date_format', function() {
|
12
|
+
var date = createFixedDate();
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
it('should default to now when a date is not provided', function() {
|
15
|
+
dateFormat.asString(dateFormat.DATETIME_FORMAT).should.not.be.empty();
|
16
|
+
});
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
it('should be usable directly without calling asString', function() {
|
19
|
+
dateFormat(dateFormat.DATETIME_FORMAT, date).should.eql('11 01 2010 14:31:30.005');
|
20
|
+
});
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
it('should format a date as string using a pattern', function() {
|
23
|
+
dateFormat.asString(dateFormat.DATETIME_FORMAT, date).should.eql('11 01 2010 14:31:30.005');
|
24
|
+
});
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
it('should default to the ISO8601 format', function() {
|
27
|
+
dateFormat.asString(date).should.eql('2010-01-11T14:31:30.005');
|
28
|
+
});
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
it('should provide a ISO8601 with timezone offset format', function() {
|
31
|
+
var tzDate = createFixedDate();
|
32
|
+
tzDate.setMinutes(tzDate.getMinutes() - tzDate.getTimezoneOffset() - 660);
|
33
|
+
tzDate.getTimezoneOffset = function () {
|
34
|
+
return -660;
|
35
|
+
};
|
35
36
|
|
36
|
-
|
37
|
+
// when tz offset is in the pattern, the date should be in UTC
|
38
|
+
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
|
39
|
+
.should.eql('2010-01-11T03:31:30.005+1100');
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
tzDate = createFixedDate();
|
42
|
+
tzDate.setMinutes((tzDate.getMinutes() - tzDate.getTimezoneOffset()) + 120);
|
43
|
+
tzDate.getTimezoneOffset = function () {
|
44
|
+
return 120;
|
45
|
+
};
|
43
46
|
|
44
|
-
|
45
|
-
|
47
|
+
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
|
48
|
+
.should.eql('2010-01-11T16:31:30.005-0200');
|
49
|
+
});
|
46
50
|
|
47
|
-
|
48
|
-
|
49
|
-
|
51
|
+
it('should provide a just-the-time format', function() {
|
52
|
+
dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
|
53
|
+
});
|
50
54
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
55
|
+
it('should provide a custom format', function() {
|
56
|
+
var customDate = createFixedDate();
|
57
|
+
customDate.setMinutes((customDate.getMinutes() - customDate.getTimezoneOffset()) + 120);
|
58
|
+
customDate.getTimezoneOffset = function () {
|
59
|
+
return 120;
|
60
|
+
};
|
57
61
|
|
58
|
-
|
59
|
-
|
62
|
+
dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.16.11.01.10');
|
63
|
+
});
|
60
64
|
});
|
@@ -0,0 +1,177 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
require("should");
|
4
|
+
var dateFormat = require("../lib");
|
5
|
+
|
6
|
+
describe("dateFormat.parse", function() {
|
7
|
+
it("should require a pattern", function() {
|
8
|
+
(function() {
|
9
|
+
dateFormat.parse();
|
10
|
+
}.should.throw(/pattern must be supplied/));
|
11
|
+
(function() {
|
12
|
+
dateFormat.parse(null);
|
13
|
+
}.should.throw(/pattern must be supplied/));
|
14
|
+
(function() {
|
15
|
+
dateFormat.parse("");
|
16
|
+
}.should.throw(/pattern must be supplied/));
|
17
|
+
});
|
18
|
+
|
19
|
+
describe("with a pattern that has no replacements", function() {
|
20
|
+
it("should return a new date when the string matches", function() {
|
21
|
+
dateFormat.parse("cheese", "cheese").should.be.a.Date();
|
22
|
+
});
|
23
|
+
|
24
|
+
it("should throw if the string does not match", function() {
|
25
|
+
(function() {
|
26
|
+
dateFormat.parse("cheese", "biscuits");
|
27
|
+
}.should.throw(/String 'biscuits' could not be parsed as 'cheese'/));
|
28
|
+
});
|
29
|
+
});
|
30
|
+
|
31
|
+
describe("with a full pattern", function() {
|
32
|
+
var pattern = "yyyy-MM-dd hh:mm:ss.SSSO";
|
33
|
+
|
34
|
+
it("should return the correct date if the string matches", function() {
|
35
|
+
var testDate = new Date();
|
36
|
+
testDate.setFullYear(2018);
|
37
|
+
testDate.setMonth(8);
|
38
|
+
testDate.setDate(13);
|
39
|
+
testDate.setHours(18);
|
40
|
+
testDate.setMinutes(10);
|
41
|
+
testDate.setSeconds(12);
|
42
|
+
testDate.setMilliseconds(392);
|
43
|
+
testDate.getTimezoneOffset = function() {
|
44
|
+
return 600;
|
45
|
+
};
|
46
|
+
|
47
|
+
dateFormat
|
48
|
+
.parse(pattern, "2018-09-13 08:10:12.392+1000")
|
49
|
+
.getTime()
|
50
|
+
.should.eql(testDate.getTime());
|
51
|
+
});
|
52
|
+
|
53
|
+
it("should throw if the string does not match", function() {
|
54
|
+
(function() {
|
55
|
+
dateFormat.parse(pattern, "biscuits");
|
56
|
+
}.should.throw(
|
57
|
+
/String 'biscuits' could not be parsed as 'yyyy-MM-dd hh:mm:ss.SSSO'/
|
58
|
+
));
|
59
|
+
});
|
60
|
+
});
|
61
|
+
|
62
|
+
describe("with a partial pattern", function() {
|
63
|
+
var testDate = new Date();
|
64
|
+
dateFormat.now = function() {
|
65
|
+
return testDate;
|
66
|
+
};
|
67
|
+
|
68
|
+
function verifyDate(actual, expected) {
|
69
|
+
actual.getFullYear().should.eql(expected.year || testDate.getFullYear());
|
70
|
+
actual.getMonth().should.eql(expected.month || testDate.getMonth());
|
71
|
+
actual.getDate().should.eql(expected.day || testDate.getDate());
|
72
|
+
actual.getHours().should.eql(expected.hours || testDate.getHours());
|
73
|
+
actual.getMinutes().should.eql(expected.minutes || testDate.getMinutes());
|
74
|
+
actual.getSeconds().should.eql(expected.seconds || testDate.getSeconds());
|
75
|
+
actual
|
76
|
+
.getMilliseconds()
|
77
|
+
.should.eql(expected.milliseconds || testDate.getMilliseconds());
|
78
|
+
}
|
79
|
+
|
80
|
+
it("should return a date with missing values defaulting to current time", function() {
|
81
|
+
var date = dateFormat.parse("yyyy-MM", "2015-09");
|
82
|
+
verifyDate(date, { year: 2015, month: 8 });
|
83
|
+
});
|
84
|
+
|
85
|
+
it("should use a passed in date for missing values", function() {
|
86
|
+
var missingValueDate = new Date(2010, 1, 11, 10, 30, 12, 100);
|
87
|
+
var date = dateFormat.parse("yyyy-MM", "2015-09", missingValueDate);
|
88
|
+
verifyDate(date, {
|
89
|
+
year: 2015,
|
90
|
+
month: 8,
|
91
|
+
day: 11,
|
92
|
+
hours: 10,
|
93
|
+
minutes: 30,
|
94
|
+
seconds: 12,
|
95
|
+
milliseconds: 100
|
96
|
+
});
|
97
|
+
});
|
98
|
+
|
99
|
+
it("should handle variations on the same pattern", function() {
|
100
|
+
var date = dateFormat.parse("MM-yyyy", "09-2015");
|
101
|
+
verifyDate(date, { year: 2015, month: 8 });
|
102
|
+
|
103
|
+
date = dateFormat.parse("yyyy MM", "2015 09");
|
104
|
+
verifyDate(date, { year: 2015, month: 8 });
|
105
|
+
|
106
|
+
date = dateFormat.parse("MM, yyyy.", "09, 2015.");
|
107
|
+
verifyDate(date, { year: 2015, month: 8 });
|
108
|
+
});
|
109
|
+
|
110
|
+
it("should match all the date parts", function() {
|
111
|
+
var date = dateFormat.parse("dd", "21");
|
112
|
+
verifyDate(date, { day: 21 });
|
113
|
+
|
114
|
+
date = dateFormat.parse("hh", "12");
|
115
|
+
verifyDate(date, { hours: 12 });
|
116
|
+
|
117
|
+
date = dateFormat.parse("mm", "34");
|
118
|
+
verifyDate(date, { minutes: 34 });
|
119
|
+
|
120
|
+
date = dateFormat.parse("ss", "59");
|
121
|
+
verifyDate(date, { seconds: 59 });
|
122
|
+
|
123
|
+
date = dateFormat.parse("ss.SSS", "23.452");
|
124
|
+
verifyDate(date, { seconds: 23, milliseconds: 452 });
|
125
|
+
|
126
|
+
date = dateFormat.parse("hh:mm O", "05:23 +1000");
|
127
|
+
verifyDate(date, { hours: 15, minutes: 23 });
|
128
|
+
|
129
|
+
date = dateFormat.parse("hh:mm O", "05:23 -200");
|
130
|
+
verifyDate(date, { hours: 3, minutes: 23 });
|
131
|
+
|
132
|
+
date = dateFormat.parse("hh:mm O", "05:23 +0930");
|
133
|
+
verifyDate(date, { hours: 14, minutes: 53 });
|
134
|
+
});
|
135
|
+
});
|
136
|
+
|
137
|
+
describe("with a date formatted by this library", function() {
|
138
|
+
var testDate = new Date();
|
139
|
+
testDate.setUTCFullYear(2018);
|
140
|
+
testDate.setUTCMonth(8);
|
141
|
+
testDate.setUTCDate(13);
|
142
|
+
testDate.setUTCHours(18);
|
143
|
+
testDate.setUTCMinutes(10);
|
144
|
+
testDate.setUTCSeconds(12);
|
145
|
+
testDate.setUTCMilliseconds(392);
|
146
|
+
|
147
|
+
it("should format and then parse back to the same date", function() {
|
148
|
+
dateFormat
|
149
|
+
.parse(
|
150
|
+
dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
|
151
|
+
dateFormat(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, testDate)
|
152
|
+
)
|
153
|
+
.should.eql(testDate);
|
154
|
+
|
155
|
+
dateFormat
|
156
|
+
.parse(
|
157
|
+
dateFormat.ISO8601_FORMAT,
|
158
|
+
dateFormat(dateFormat.ISO8601_FORMAT, testDate)
|
159
|
+
)
|
160
|
+
.should.eql(testDate);
|
161
|
+
|
162
|
+
dateFormat
|
163
|
+
.parse(
|
164
|
+
dateFormat.DATETIME_FORMAT,
|
165
|
+
dateFormat(dateFormat.DATETIME_FORMAT, testDate)
|
166
|
+
)
|
167
|
+
.should.eql(testDate);
|
168
|
+
|
169
|
+
dateFormat
|
170
|
+
.parse(
|
171
|
+
dateFormat.ABSOLUTETIME_FORMAT,
|
172
|
+
dateFormat(dateFormat.ABSOLUTETIME_FORMAT, testDate)
|
173
|
+
)
|
174
|
+
.should.eql(testDate);
|
175
|
+
});
|
176
|
+
});
|
177
|
+
});
|