date-format 1.1.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc +5 -17
- package/.travis.yml +2 -3
- package/README.md +22 -4
- package/lib/index.js +191 -49
- package/package.json +5 -6
- package/test/date_format-test.js +4 -6
- package/test/parse-test.js +220 -0
- package/.npmignore +0 -15
package/.eslintrc
CHANGED
@@ -1,24 +1,12 @@
|
|
1
1
|
{
|
2
|
+
"extends": [
|
3
|
+
"eslint:recommended"
|
4
|
+
],
|
2
5
|
"env": {
|
3
6
|
"node": true,
|
4
7
|
"mocha": true
|
5
8
|
},
|
6
9
|
"plugins": [
|
7
|
-
"mocha"
|
8
|
-
|
9
|
-
],
|
10
|
-
"rules": {
|
11
|
-
"comma-dangle": 0,
|
12
|
-
"indent": 2,
|
13
|
-
"func-names": 0,
|
14
|
-
"max-len": [1, 120, 2],
|
15
|
-
"no-use-before-define": 1,
|
16
|
-
"no-param-reassign": 0,
|
17
|
-
"strict": 0,
|
18
|
-
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["test/*.js"]}],
|
19
|
-
"mocha/no-exclusive-tests": "error"
|
20
|
-
},
|
21
|
-
"parserOptions": {
|
22
|
-
"ecmaVersion": 5
|
23
|
-
}
|
10
|
+
"mocha"
|
11
|
+
]
|
24
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,15 +1,15 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
2
|
|
3
3
|
function padWithZeros(vNumber, width) {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
var numAsString = vNumber.toString();
|
5
|
+
while (numAsString.length < width) {
|
6
|
+
numAsString = "0" + numAsString;
|
7
|
+
}
|
8
|
+
return numAsString;
|
9
9
|
}
|
10
10
|
|
11
11
|
function addZero(vNumber) {
|
12
|
-
|
12
|
+
return padWithZeros(vNumber, 2);
|
13
13
|
}
|
14
14
|
|
15
15
|
/**
|
@@ -18,46 +18,42 @@ function addZero(vNumber) {
|
|
18
18
|
* @private
|
19
19
|
*/
|
20
20
|
function offset(timezoneOffset) {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
return timezoneOffset < 0 ? '+' + h + m : '-' + h + m;
|
21
|
+
var os = Math.abs(timezoneOffset);
|
22
|
+
var h = String(Math.floor(os / 60));
|
23
|
+
var m = String(os % 60);
|
24
|
+
if (h.length === 1) {
|
25
|
+
h = "0" + h;
|
26
|
+
}
|
27
|
+
if (m.length === 1) {
|
28
|
+
m = "0" + m;
|
29
|
+
}
|
30
|
+
return timezoneOffset < 0 ? "+" + h + m : "-" + h + m;
|
32
31
|
}
|
33
32
|
|
34
|
-
function asString(format, date
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
}
|
43
|
-
// make the date independent of the system timezone by working with UTC
|
44
|
-
if (timezoneOffset === undefined) {
|
45
|
-
timezoneOffset = date.getTimezoneOffset();
|
46
|
-
}
|
33
|
+
function asString(format, date) {
|
34
|
+
if (typeof format !== "string") {
|
35
|
+
date = format;
|
36
|
+
format = module.exports.ISO8601_FORMAT;
|
37
|
+
}
|
38
|
+
if (!date) {
|
39
|
+
date = module.exports.now();
|
40
|
+
}
|
47
41
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
42
|
+
// Issue # 14 - Per ISO8601 standard, the time string should be local time
|
43
|
+
// with timezone info.
|
44
|
+
// See https://en.wikipedia.org/wiki/ISO_8601 section "Time offsets from UTC"
|
45
|
+
|
46
|
+
var vDay = addZero(date.getDate());
|
47
|
+
var vMonth = addZero(date.getMonth() + 1);
|
48
|
+
var vYearLong = addZero(date.getFullYear());
|
49
|
+
var vYearShort = addZero(vYearLong.substring(2, 4));
|
50
|
+
var vYear = format.indexOf("yyyy") > -1 ? vYearLong : vYearShort;
|
51
|
+
var vHour = addZero(date.getHours());
|
52
|
+
var vMinute = addZero(date.getMinutes());
|
53
|
+
var vSecond = addZero(date.getSeconds());
|
54
|
+
var vMillisecond = padWithZeros(date.getMilliseconds(), 3);
|
55
|
+
var vTimeZone = offset(date.getTimezoneOffset());
|
56
|
+
var formatted = format
|
61
57
|
.replace(/dd/g, vDay)
|
62
58
|
.replace(/MM/g, vMonth)
|
63
59
|
.replace(/y{1,4}/g, vYear)
|
@@ -66,12 +62,158 @@ function asString(format, date, timezoneOffset) {
|
|
66
62
|
.replace(/ss/g, vSecond)
|
67
63
|
.replace(/SSS/g, vMillisecond)
|
68
64
|
.replace(/O/g, vTimeZone);
|
69
|
-
|
65
|
+
return formatted;
|
66
|
+
}
|
67
|
+
|
68
|
+
function setDatePart(date, part, value, local) {
|
69
|
+
date['set' + (local ? '' : 'UTC') + part](value);
|
70
|
+
}
|
71
|
+
|
72
|
+
function extractDateParts(pattern, str, missingValuesDate) {
|
73
|
+
// Javascript Date object doesn't support custom timezone. Sets all felds as
|
74
|
+
// GMT based to begin with. If the timezone offset is provided, then adjust
|
75
|
+
// it using provided timezone, otherwise, adjust it with the system timezone.
|
76
|
+
var local = pattern.indexOf('O') < 0;
|
77
|
+
var matchers = [
|
78
|
+
{
|
79
|
+
pattern: /y{1,4}/,
|
80
|
+
regexp: "\\d{1,4}",
|
81
|
+
fn: function(date, value) {
|
82
|
+
setDatePart(date, 'FullYear', value, local);
|
83
|
+
}
|
84
|
+
},
|
85
|
+
{
|
86
|
+
pattern: /MM/,
|
87
|
+
regexp: "\\d{1,2}",
|
88
|
+
fn: function(date, value) {
|
89
|
+
setDatePart(date, 'Month', (value - 1), local);
|
90
|
+
}
|
91
|
+
},
|
92
|
+
{
|
93
|
+
pattern: /dd/,
|
94
|
+
regexp: "\\d{1,2}",
|
95
|
+
fn: function(date, value) {
|
96
|
+
setDatePart(date, 'Date', value, local);
|
97
|
+
}
|
98
|
+
},
|
99
|
+
{
|
100
|
+
pattern: /hh/,
|
101
|
+
regexp: "\\d{1,2}",
|
102
|
+
fn: function(date, value) {
|
103
|
+
setDatePart(date, 'Hours', value, local);
|
104
|
+
}
|
105
|
+
},
|
106
|
+
{
|
107
|
+
pattern: /mm/,
|
108
|
+
regexp: "\\d\\d",
|
109
|
+
fn: function(date, value) {
|
110
|
+
setDatePart(date, 'Minutes', value, local);
|
111
|
+
}
|
112
|
+
},
|
113
|
+
{
|
114
|
+
pattern: /ss/,
|
115
|
+
regexp: "\\d\\d",
|
116
|
+
fn: function(date, value) {
|
117
|
+
setDatePart(date, 'Seconds', value, local);
|
118
|
+
}
|
119
|
+
},
|
120
|
+
{
|
121
|
+
pattern: /SSS/,
|
122
|
+
regexp: "\\d\\d\\d",
|
123
|
+
fn: function(date, value) {
|
124
|
+
setDatePart(date, 'Milliseconds', value, local);
|
125
|
+
}
|
126
|
+
},
|
127
|
+
{
|
128
|
+
pattern: /O/,
|
129
|
+
regexp: "[+-]\\d{3,4}|Z",
|
130
|
+
fn: function(date, value) {
|
131
|
+
if (value === "Z") {
|
132
|
+
value = 0;
|
133
|
+
}
|
134
|
+
var offset = Math.abs(value);
|
135
|
+
var timezoneOffset = (value > 0 ? -1 : 1 ) * ((offset % 100) + Math.floor(offset / 100) * 60);
|
136
|
+
// Per ISO8601 standard: UTC = local time - offset
|
137
|
+
//
|
138
|
+
// For example, 2000-01-01T01:00:00-0700
|
139
|
+
// local time: 2000-01-01T01:00:00
|
140
|
+
// ==> UTC : 2000-01-01T08:00:00 ( 01 - (-7) = 8 )
|
141
|
+
//
|
142
|
+
// To make it even more confusing, the date.getTimezoneOffset() is
|
143
|
+
// opposite sign of offset string in the ISO8601 standard. So if offset
|
144
|
+
// is '-0700' the getTimezoneOffset() would be (+)420. The line above
|
145
|
+
// calculates timezoneOffset to matche Javascript's behavior.
|
146
|
+
//
|
147
|
+
// The date/time of the input is actually the local time, so the date
|
148
|
+
// object that was constructed is actually local time even thought the
|
149
|
+
// UTC setters are used. This means the date object's internal UTC
|
150
|
+
// representation was wrong. It needs to be fixed by substracting the
|
151
|
+
// offset (or adding the offset minutes as they are opposite sign).
|
152
|
+
//
|
153
|
+
// Note: the time zone has to be processed after all other fileds are
|
154
|
+
// set. The result would be incorrect if the offset was calculated
|
155
|
+
// first then overriden by the other filed setters.
|
156
|
+
date.setUTCMinutes(date.getUTCMinutes() + timezoneOffset);
|
157
|
+
}
|
158
|
+
}
|
159
|
+
];
|
160
|
+
|
161
|
+
var parsedPattern = matchers.reduce(
|
162
|
+
function(p, m) {
|
163
|
+
if (m.pattern.test(p.regexp)) {
|
164
|
+
m.index = p.regexp.match(m.pattern).index;
|
165
|
+
p.regexp = p.regexp.replace(m.pattern, "(" + m.regexp + ")");
|
166
|
+
} else {
|
167
|
+
m.index = -1;
|
168
|
+
}
|
169
|
+
return p;
|
170
|
+
},
|
171
|
+
{ regexp: pattern, index: [] }
|
172
|
+
);
|
173
|
+
|
174
|
+
var dateFns = matchers.filter(function(m) {
|
175
|
+
return m.index > -1;
|
176
|
+
});
|
177
|
+
dateFns.sort(function(a, b) {
|
178
|
+
return a.index - b.index;
|
179
|
+
});
|
180
|
+
|
181
|
+
var matcher = new RegExp(parsedPattern.regexp);
|
182
|
+
var matches = matcher.exec(str);
|
183
|
+
if (matches) {
|
184
|
+
var date = missingValuesDate || module.exports.now();
|
185
|
+
dateFns.forEach(function(f, i) {
|
186
|
+
f.fn(date, matches[i + 1]);
|
187
|
+
});
|
188
|
+
|
189
|
+
return date;
|
190
|
+
}
|
191
|
+
|
192
|
+
throw new Error(
|
193
|
+
"String '" + str + "' could not be parsed as '" + pattern + "'"
|
194
|
+
);
|
195
|
+
}
|
196
|
+
|
197
|
+
function parse(pattern, str, missingValuesDate) {
|
198
|
+
if (!pattern) {
|
199
|
+
throw new Error("pattern must be supplied");
|
200
|
+
}
|
201
|
+
|
202
|
+
return extractDateParts(pattern, str, missingValuesDate);
|
203
|
+
}
|
204
|
+
|
205
|
+
/**
|
206
|
+
* Used for testing - replace this function with a fixed date.
|
207
|
+
*/
|
208
|
+
function now() {
|
209
|
+
return new Date();
|
70
210
|
}
|
71
211
|
|
72
212
|
module.exports = asString;
|
73
213
|
module.exports.asString = asString;
|
74
|
-
module.exports.
|
75
|
-
module.exports.
|
76
|
-
module.exports.
|
77
|
-
module.exports.
|
214
|
+
module.exports.parse = parse;
|
215
|
+
module.exports.now = now;
|
216
|
+
module.exports.ISO8601_FORMAT = "yyyy-MM-ddThh:mm:ss.SSS";
|
217
|
+
module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = "yyyy-MM-ddThh:mm:ss.SSSO";
|
218
|
+
module.exports.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS";
|
219
|
+
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": "
|
3
|
+
"version": "3.0.0",
|
4
4
|
"description": "Formatting Date objects as strings since 2013",
|
5
5
|
"main": "lib/index.js",
|
6
6
|
"repository": {
|
@@ -25,10 +25,9 @@
|
|
25
25
|
"readmeFilename": "README.md",
|
26
26
|
"gitHead": "bf59015ab6c9e86454b179374f29debbdb403522",
|
27
27
|
"devDependencies": {
|
28
|
-
"eslint": "^
|
29
|
-
"eslint-plugin-
|
30
|
-
"
|
31
|
-
"
|
32
|
-
"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"
|
33
32
|
}
|
34
33
|
}
|
package/test/date_format-test.js
CHANGED
@@ -24,27 +24,26 @@ describe('date_format', function() {
|
|
24
24
|
});
|
25
25
|
|
26
26
|
it('should default to the ISO8601 format', function() {
|
27
|
-
dateFormat.asString(date).should.eql('2010-01-
|
27
|
+
dateFormat.asString(date).should.eql('2010-01-11T14:31:30.005');
|
28
28
|
});
|
29
29
|
|
30
30
|
it('should provide a ISO8601 with timezone offset format', function() {
|
31
31
|
var tzDate = createFixedDate();
|
32
|
-
tzDate.setMinutes(tzDate.getMinutes() - tzDate.getTimezoneOffset() - 660);
|
33
32
|
tzDate.getTimezoneOffset = function () {
|
34
33
|
return -660;
|
35
34
|
};
|
36
35
|
|
36
|
+
// when tz offset is in the pattern, the date should be in local time
|
37
37
|
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
|
38
|
-
|
38
|
+
.should.eql('2010-01-11T14:31:30.005+1100');
|
39
39
|
|
40
40
|
tzDate = createFixedDate();
|
41
|
-
tzDate.setMinutes((tzDate.getMinutes() - tzDate.getTimezoneOffset()) + 120);
|
42
41
|
tzDate.getTimezoneOffset = function () {
|
43
42
|
return 120;
|
44
43
|
};
|
45
44
|
|
46
45
|
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
|
47
|
-
|
46
|
+
.should.eql('2010-01-11T14:31:30.005-0200');
|
48
47
|
});
|
49
48
|
|
50
49
|
it('should provide a just-the-time format', function() {
|
@@ -53,7 +52,6 @@ describe('date_format', function() {
|
|
53
52
|
|
54
53
|
it('should provide a custom format', function() {
|
55
54
|
var customDate = createFixedDate();
|
56
|
-
customDate.setMinutes((customDate.getMinutes() - customDate.getTimezoneOffset()) + 120);
|
57
55
|
customDate.getTimezoneOffset = function () {
|
58
56
|
return 120;
|
59
57
|
};
|
@@ -0,0 +1,220 @@
|
|
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.setUTCFullYear(2018);
|
37
|
+
testDate.setUTCMonth(8);
|
38
|
+
testDate.setUTCDate(13);
|
39
|
+
testDate.setUTCHours(18);
|
40
|
+
testDate.setUTCMinutes(10);
|
41
|
+
testDate.setUTCSeconds(12);
|
42
|
+
testDate.setUTCMilliseconds(392);
|
43
|
+
|
44
|
+
dateFormat
|
45
|
+
.parse(pattern, "2018-09-14 04:10:12.392+1000")
|
46
|
+
.getTime()
|
47
|
+
.should.eql(testDate.getTime())
|
48
|
+
;
|
49
|
+
});
|
50
|
+
|
51
|
+
it("should throw if the string does not match", function() {
|
52
|
+
(function() {
|
53
|
+
dateFormat.parse(pattern, "biscuits");
|
54
|
+
}.should.throw(
|
55
|
+
/String 'biscuits' could not be parsed as 'yyyy-MM-dd hh:mm:ss.SSSO'/
|
56
|
+
));
|
57
|
+
});
|
58
|
+
});
|
59
|
+
|
60
|
+
describe("with a partial pattern", function() {
|
61
|
+
var testDate = new Date();
|
62
|
+
dateFormat.now = function() {
|
63
|
+
return testDate;
|
64
|
+
};
|
65
|
+
|
66
|
+
/**
|
67
|
+
* If there's no timezone in the format, then we verify against the local date
|
68
|
+
*/
|
69
|
+
function verifyLocalDate(actual, expected) {
|
70
|
+
actual.getFullYear().should.eql(expected.year || testDate.getFullYear());
|
71
|
+
actual.getMonth().should.eql(expected.month || testDate.getMonth());
|
72
|
+
actual.getDate().should.eql(expected.day || testDate.getDate());
|
73
|
+
actual.getHours().should.eql(expected.hours || testDate.getHours());
|
74
|
+
actual.getMinutes().should.eql(expected.minutes || testDate.getMinutes());
|
75
|
+
actual.getSeconds().should.eql(expected.seconds || testDate.getSeconds());
|
76
|
+
actual
|
77
|
+
.getMilliseconds()
|
78
|
+
.should.eql(expected.milliseconds || testDate.getMilliseconds());
|
79
|
+
}
|
80
|
+
|
81
|
+
/**
|
82
|
+
* If a timezone is specified, let's verify against the UTC time it is supposed to be
|
83
|
+
*/
|
84
|
+
function verifyDate(actual, expected) {
|
85
|
+
actual.getUTCFullYear().should.eql(expected.year || testDate.getUTCFullYear());
|
86
|
+
actual.getUTCMonth().should.eql(expected.month || testDate.getUTCMonth());
|
87
|
+
actual.getUTCDate().should.eql(expected.day || testDate.getUTCDate());
|
88
|
+
actual.getUTCHours().should.eql(expected.hours || testDate.getUTCHours());
|
89
|
+
actual.getUTCMinutes().should.eql(expected.minutes || testDate.getUTCMinutes());
|
90
|
+
actual.getUTCSeconds().should.eql(expected.seconds || testDate.getUTCSeconds());
|
91
|
+
actual
|
92
|
+
.getMilliseconds()
|
93
|
+
.should.eql(expected.milliseconds || testDate.getMilliseconds());
|
94
|
+
}
|
95
|
+
|
96
|
+
it("should return a date with missing values defaulting to current time", function() {
|
97
|
+
var date = dateFormat.parse("yyyy-MM", "2015-09");
|
98
|
+
verifyLocalDate(date, { year: 2015, month: 8 });
|
99
|
+
});
|
100
|
+
|
101
|
+
it("should use a passed in date for missing values", function() {
|
102
|
+
var missingValueDate = new Date(2010, 1, 8, 22, 30, 12, 100);
|
103
|
+
var date = dateFormat.parse("yyyy-MM", "2015-09", missingValueDate);
|
104
|
+
verifyLocalDate(date, {
|
105
|
+
year: 2015,
|
106
|
+
month: 8,
|
107
|
+
day: 8,
|
108
|
+
hours: 22,
|
109
|
+
minutes: 30,
|
110
|
+
seconds: 12,
|
111
|
+
milliseconds: 100
|
112
|
+
});
|
113
|
+
});
|
114
|
+
|
115
|
+
it("should handle variations on the same pattern", function() {
|
116
|
+
var date = dateFormat.parse("MM-yyyy", "09-2015");
|
117
|
+
verifyLocalDate(date, { year: 2015, month: 8 });
|
118
|
+
|
119
|
+
date = dateFormat.parse("yyyy MM", "2015 09");
|
120
|
+
verifyLocalDate(date, { year: 2015, month: 8 });
|
121
|
+
|
122
|
+
date = dateFormat.parse("MM, yyyy.", "09, 2015.");
|
123
|
+
verifyLocalDate(date, { year: 2015, month: 8 });
|
124
|
+
});
|
125
|
+
|
126
|
+
describe("should match all the date parts", function() {
|
127
|
+
it("works with dd", function() {
|
128
|
+
var date = dateFormat.parse("dd", "21");
|
129
|
+
verifyLocalDate(date, { day: 21 });
|
130
|
+
});
|
131
|
+
|
132
|
+
it("works with hh", function() {
|
133
|
+
var date = dateFormat.parse("hh", "12");
|
134
|
+
verifyLocalDate(date, { hours: 12 });
|
135
|
+
});
|
136
|
+
|
137
|
+
it("works with mm", function() {
|
138
|
+
var date = dateFormat.parse("mm", "34");
|
139
|
+
verifyLocalDate(date, { minutes: 34 });
|
140
|
+
});
|
141
|
+
|
142
|
+
it("works with ss", function() {
|
143
|
+
var date = dateFormat.parse("ss", "59");
|
144
|
+
verifyLocalDate(date, { seconds: 59 });
|
145
|
+
});
|
146
|
+
|
147
|
+
it("works with ss.SSS", function() {
|
148
|
+
var date = dateFormat.parse("ss.SSS", "23.452");
|
149
|
+
verifyLocalDate(date, { seconds: 23, milliseconds: 452 });
|
150
|
+
});
|
151
|
+
|
152
|
+
it("works with hh:mm O (+1000)", function() {
|
153
|
+
var date = dateFormat.parse("hh:mm O", "05:23 +1000");
|
154
|
+
verifyDate(date, { hours: 19, minutes: 23 });
|
155
|
+
});
|
156
|
+
|
157
|
+
it("works with hh:mm O (-200)", function() {
|
158
|
+
var date = dateFormat.parse("hh:mm O", "05:23 -200");
|
159
|
+
verifyDate(date, { hours: 7, minutes: 23 });
|
160
|
+
});
|
161
|
+
|
162
|
+
it("works with hh:mm O (+0930)", function() {
|
163
|
+
var date = dateFormat.parse("hh:mm O", "05:23 +0930");
|
164
|
+
verifyDate(date, { hours: 19, minutes: 53 });
|
165
|
+
});
|
166
|
+
});
|
167
|
+
});
|
168
|
+
|
169
|
+
describe("with a date formatted by this library", function() {
|
170
|
+
describe("should format and then parse back to the same date", function() {
|
171
|
+
function testDateInitWithUTC() {
|
172
|
+
var td = new Date();
|
173
|
+
td.setUTCFullYear(2018);
|
174
|
+
td.setUTCMonth(8);
|
175
|
+
td.setUTCDate(13);
|
176
|
+
td.setUTCHours(18);
|
177
|
+
td.setUTCMinutes(10);
|
178
|
+
td.setUTCSeconds(12);
|
179
|
+
td.setUTCMilliseconds(392);
|
180
|
+
return td;
|
181
|
+
}
|
182
|
+
|
183
|
+
it("works with ISO8601_WITH_TZ_OFFSET_FORMAT", function() {
|
184
|
+
// For this test case to work, the date object must be initialized with
|
185
|
+
// UTC timezone
|
186
|
+
var td = testDateInitWithUTC();
|
187
|
+
var d = dateFormat(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, td);
|
188
|
+
dateFormat.parse(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, d)
|
189
|
+
.should.eql(td);
|
190
|
+
});
|
191
|
+
|
192
|
+
it("works with ISO8601_FORMAT", function() {
|
193
|
+
var td = new Date();
|
194
|
+
var d = dateFormat(dateFormat.ISO8601_FORMAT, td);
|
195
|
+
var actual = dateFormat.parse(dateFormat.ISO8601_FORMAT, d);
|
196
|
+
actual.should.eql(td);
|
197
|
+
});
|
198
|
+
|
199
|
+
it("works with DATETIME_FORMAT", function() {
|
200
|
+
var testDate = new Date();
|
201
|
+
dateFormat
|
202
|
+
.parse(
|
203
|
+
dateFormat.DATETIME_FORMAT,
|
204
|
+
dateFormat(dateFormat.DATETIME_FORMAT, testDate)
|
205
|
+
)
|
206
|
+
.should.eql(testDate);
|
207
|
+
});
|
208
|
+
|
209
|
+
it("works with ABSOLUTETIME_FORMAT", function() {
|
210
|
+
var testDate = new Date();
|
211
|
+
dateFormat
|
212
|
+
.parse(
|
213
|
+
dateFormat.ABSOLUTETIME_FORMAT,
|
214
|
+
dateFormat(dateFormat.ABSOLUTETIME_FORMAT, testDate)
|
215
|
+
)
|
216
|
+
.should.eql(testDate);
|
217
|
+
});
|
218
|
+
});
|
219
|
+
});
|
220
|
+
});
|