date-format 2.0.0 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,18 +15,28 @@ Formatting dates as strings
15
15
 
16
16
  ```javascript
17
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
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
+ format.asString(format.ISO8601_WITH_TZ_OFFSET_FORMAT, new Date()); // in ISO8601 with timezone
21
22
  ```
22
23
 
23
24
  or
24
25
 
25
26
  ```javascript
26
27
  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());
28
+ format(); // defaults to ISO8601 format and current date
29
+ format(new Date()); // defaults to ISO8601 format
30
+ format('hh:mm:ss.SSS', new Date()); // just the time
31
+ format(format.ISO8601_WITH_TZ_OFFSET_FORMAT, new Date()); // in ISO8601 with timezone
32
+ ```
33
+
34
+ **output:**
35
+ ```javascript
36
+ 2017-03-14T14:10:20.391
37
+ 2017-03-14T14:10:20.391
38
+ 14:10:20.391
39
+ 2017-03-14T14:10:20.391+11:00
30
40
  ```
31
41
 
32
42
  Format string can be anything, but the following letters will be replaced (and leading zeroes added if necessary):
@@ -38,11 +48,11 @@ Format string can be anything, but the following letters will be replaced (and l
38
48
  * mm - `date.getMinutes()`
39
49
  * ss - `date.getSeconds()`
40
50
  * SSS - `date.getMilliseconds()`
41
- * O - timezone offset in +hm format (note that time will be in UTC if displaying offset)
51
+ * O - timezone offset in ±hh:mm format (note that time will still be local if displaying offset)
42
52
 
43
53
  Built-in formats:
44
54
  * `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)
55
+ * `format.ISO8601_WITH_TZ_OFFSET_FORMAT` - `2017-03-14T14:10:20.391+11:00` (local + TZ used)
46
56
  * `format.DATETIME_FORMAT` - `14 03 2017 14:10:20.391` (local time used)
47
57
  * `format.ABSOLUTETIME_FORMAT` - `14:10:20.391` (local time used)
48
58
 
@@ -54,5 +64,8 @@ The date format library has limited ability to parse strings into dates. It can
54
64
  var format = require('date-format');
55
65
  // pass in the format of the string as first argument
56
66
  format.parse(format.ISO8601_FORMAT, '2017-03-14T14:10:20.391');
67
+ format.parse(format.ISO8601_WITH_TZ_OFFSET_FORMAT, '2017-03-14T14:10:20.391+1100');
68
+ format.parse(format.ISO8601_WITH_TZ_OFFSET_FORMAT, '2017-03-14T14:10:20.391+11:00');
69
+ format.parse(format.ISO8601_WITH_TZ_OFFSET_FORMAT, '2017-03-14T03:10:20.391Z');
57
70
  // returns Date
58
71
  ```
package/lib/index.js CHANGED
@@ -1,15 +1,15 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  function padWithZeros(vNumber, width) {
4
- var numAsString = vNumber.toString();
5
- while (numAsString.length < width) {
6
- numAsString = '0' + numAsString;
7
- }
8
- return numAsString;
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
- return padWithZeros(vNumber, 2);
12
+ return padWithZeros(vNumber, 2);
13
13
  }
14
14
 
15
15
  /**
@@ -18,108 +18,191 @@ function addZero(vNumber) {
18
18
  * @private
19
19
  */
20
20
  function offset(timezoneOffset) {
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;
31
- }
32
-
33
- function datePart(date, displayUTC, part) {
34
- return displayUTC ? date['getUTC' + part]() : date['get' + part]();
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 ? "Z" : (timezoneOffset < 0 ? "+" : "-") + h + ":" + m;
35
31
  }
36
32
 
37
33
  function asString(format, date) {
38
- if (typeof format !== 'string') {
39
- date = format;
40
- format = module.exports.ISO8601_FORMAT;
41
- }
42
- if (!date) {
43
- date = module.exports.now();
44
- }
34
+ if (typeof format !== "string") {
35
+ date = format;
36
+ format = module.exports.ISO8601_FORMAT;
37
+ }
38
+ if (!date) {
39
+ date = module.exports.now();
40
+ }
41
+
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
57
+ .replace(/dd/g, vDay)
58
+ .replace(/MM/g, vMonth)
59
+ .replace(/y{1,4}/g, vYear)
60
+ .replace(/hh/g, vHour)
61
+ .replace(/mm/g, vMinute)
62
+ .replace(/ss/g, vSecond)
63
+ .replace(/SSS/g, vMillisecond)
64
+ .replace(/O/g, vTimeZone);
65
+ return formatted;
66
+ }
45
67
 
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(datePart(date, displayUTC, 'Milliseconds'), 3);
57
- var vTimeZone = offset(date.getTimezoneOffset());
58
- var formatted = format
59
- .replace(/dd/g, vDay)
60
- .replace(/MM/g, vMonth)
61
- .replace(/y{1,4}/g, vYear)
62
- .replace(/hh/g, vHour)
63
- .replace(/mm/g, vMinute)
64
- .replace(/ss/g, vSecond)
65
- .replace(/SSS/g, vMillisecond)
66
- .replace(/O/g, vTimeZone);
67
- return formatted;
68
+ function setDatePart(date, part, value, local) {
69
+ date['set' + (local ? '' : 'UTC') + part](value);
68
70
  }
69
71
 
70
- function extractDateParts(pattern, str) {
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;
71
77
  var matchers = [
72
- { pattern: /y{1,4}/, regexp: "\\d{1,4}", fn: function(date, value) { date.setFullYear(value); } },
73
- { pattern: /MM/, regexp: "\\d{1,2}", fn: function(date, value) { date.setMonth(value -1); } },
74
- { pattern: /dd/, regexp: "\\d{1,2}", fn: function(date, value) { date.setDate(value); } },
75
- { pattern: /hh/, regexp: "\\d{1,2}", fn: function(date, value) { date.setHours(value); } },
76
- { pattern: /mm/, regexp: "\\d\\d", fn: function(date, value) { date.setMinutes(value); } },
77
- { pattern: /ss/, regexp: "\\d\\d", fn: function(date, value) { date.setSeconds(value); } },
78
- { pattern: /SSS/, regexp: "\\d\\d\\d", fn: function(date, value) { date.setMilliseconds(value); } },
79
- { pattern: /O/, regexp: "[+-]\\d{3,4}|Z", fn: function(date, value) {
80
- if (value === 'Z') {
81
- value = 0;
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);
82
125
  }
83
- var offset = Math.abs(value);
84
- var minutes = (offset % 100) + (Math.floor(offset / 100) * 60);
85
- date.setMinutes(date.getMinutes() + (value > 0 ? minutes : -minutes));
86
- } }
126
+ },
127
+ {
128
+ pattern: /O/,
129
+ regexp: "[+-]\\d{1,2}:?\\d{2}?|Z",
130
+ fn: function(date, value) {
131
+ if (value === "Z") {
132
+ value = 0;
133
+ }
134
+ else {
135
+ value = value.replace(":", "");
136
+ }
137
+ var offset = Math.abs(value);
138
+ var timezoneOffset = (value > 0 ? -1 : 1 ) * ((offset % 100) + Math.floor(offset / 100) * 60);
139
+ // Per ISO8601 standard: UTC = local time - offset
140
+ //
141
+ // For example, 2000-01-01T01:00:00-0700
142
+ // local time: 2000-01-01T01:00:00
143
+ // ==> UTC : 2000-01-01T08:00:00 ( 01 - (-7) = 8 )
144
+ //
145
+ // To make it even more confusing, the date.getTimezoneOffset() is
146
+ // opposite sign of offset string in the ISO8601 standard. So if offset
147
+ // is '-0700' the getTimezoneOffset() would be (+)420. The line above
148
+ // calculates timezoneOffset to matche Javascript's behavior.
149
+ //
150
+ // The date/time of the input is actually the local time, so the date
151
+ // object that was constructed is actually local time even thought the
152
+ // UTC setters are used. This means the date object's internal UTC
153
+ // representation was wrong. It needs to be fixed by substracting the
154
+ // offset (or adding the offset minutes as they are opposite sign).
155
+ //
156
+ // Note: the time zone has to be processed after all other fileds are
157
+ // set. The result would be incorrect if the offset was calculated
158
+ // first then overriden by the other filed setters.
159
+ date.setUTCMinutes(date.getUTCMinutes() + timezoneOffset);
160
+ }
161
+ }
87
162
  ];
88
163
 
89
- var parsedPattern = matchers.reduce(function(p, m) {
90
- if (m.pattern.test(p.regexp)) {
91
- m.index = p.regexp.match(m.pattern).index;
92
- p.regexp = p.regexp.replace(m.pattern, "(" + m.regexp + ")");
93
- } else {
94
- m.index = -1;
95
- }
96
- return p;
97
- }, { regexp: pattern, index: [] });
164
+ var parsedPattern = matchers.reduce(
165
+ function(p, m) {
166
+ if (m.pattern.test(p.regexp)) {
167
+ m.index = p.regexp.match(m.pattern).index;
168
+ p.regexp = p.regexp.replace(m.pattern, "(" + m.regexp + ")");
169
+ } else {
170
+ m.index = -1;
171
+ }
172
+ return p;
173
+ },
174
+ { regexp: pattern, index: [] }
175
+ );
98
176
 
99
177
  var dateFns = matchers.filter(function(m) {
100
178
  return m.index > -1;
101
179
  });
102
- dateFns.sort(function(a, b) { return a.index - b.index; });
180
+ dateFns.sort(function(a, b) {
181
+ return a.index - b.index;
182
+ });
103
183
 
104
184
  var matcher = new RegExp(parsedPattern.regexp);
105
185
  var matches = matcher.exec(str);
106
186
  if (matches) {
107
- var date = module.exports.now();
187
+ var date = missingValuesDate || module.exports.now();
108
188
  dateFns.forEach(function(f, i) {
109
- f.fn(date, matches[i+1]);
189
+ f.fn(date, matches[i + 1]);
110
190
  });
191
+
111
192
  return date;
112
193
  }
113
194
 
114
- throw new Error('String \'' + str + '\' could not be parsed as \'' + pattern + '\'');
195
+ throw new Error(
196
+ "String '" + str + "' could not be parsed as '" + pattern + "'"
197
+ );
115
198
  }
116
199
 
117
- function parse(pattern, str) {
200
+ function parse(pattern, str, missingValuesDate) {
118
201
  if (!pattern) {
119
- throw new Error('pattern must be supplied');
202
+ throw new Error("pattern must be supplied");
120
203
  }
121
204
 
122
- return extractDateParts(pattern, str);
205
+ return extractDateParts(pattern, str, missingValuesDate);
123
206
  }
124
207
 
125
208
  /**
@@ -133,7 +216,7 @@ module.exports = asString;
133
216
  module.exports.asString = asString;
134
217
  module.exports.parse = parse;
135
218
  module.exports.now = now;
136
- module.exports.ISO8601_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSS';
137
- module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSSO';
138
- module.exports.DATETIME_FORMAT = 'dd MM yyyy hh:mm:ss.SSS';
139
- module.exports.ABSOLUTETIME_FORMAT = 'hh:mm:ss.SSS';
219
+ module.exports.ISO8601_FORMAT = "yyyy-MM-ddThh:mm:ss.SSS";
220
+ module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = "yyyy-MM-ddThh:mm:ss.SSSO";
221
+ module.exports.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS";
222
+ module.exports.ABSOLUTETIME_FORMAT = "hh:mm:ss.SSS";
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "date-format",
3
- "version": "2.0.0",
3
+ "version": "4.0.2",
4
4
  "description": "Formatting Date objects as strings since 2013",
5
5
  "main": "lib/index.js",
6
+ "files": [
7
+ "lib"
8
+ ],
6
9
  "repository": {
7
10
  "type": "git",
8
11
  "url": "https://github.com/nomiddlename/date-format.git"
@@ -25,9 +28,9 @@
25
28
  "readmeFilename": "README.md",
26
29
  "gitHead": "bf59015ab6c9e86454b179374f29debbdb403522",
27
30
  "devDependencies": {
28
- "eslint": "^5.5.0",
29
- "eslint-plugin-mocha": "^5.2.0",
30
- "mocha": "^5.2.0",
31
+ "eslint": "^8.6.0",
32
+ "eslint-plugin-mocha": "^10.0.3",
33
+ "mocha": "^9.1.3",
31
34
  "should": "^13.2.3"
32
35
  }
33
36
  }
package/.eslintrc DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "extends": [
3
- "eslint:recommended"
4
- ],
5
- "env": {
6
- "node": true,
7
- "mocha": true
8
- },
9
- "plugins": [
10
- "mocha"
11
- ]
12
- }
package/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- language: node_js
2
- sudo: false
3
- node_js:
4
- - "10"
5
- - "8"
6
- - "6"
@@ -1,64 +0,0 @@
1
- 'use strict';
2
-
3
- require('should');
4
-
5
- var dateFormat = require('../lib');
6
-
7
- function createFixedDate() {
8
- return new Date(2010, 0, 11, 14, 31, 30, 5);
9
- }
10
-
11
- describe('date_format', function() {
12
- var date = createFixedDate();
13
-
14
- it('should default to now when a date is not provided', function() {
15
- dateFormat.asString(dateFormat.DATETIME_FORMAT).should.not.be.empty();
16
- });
17
-
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
- });
21
-
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
- });
25
-
26
- it('should default to the ISO8601 format', function() {
27
- dateFormat.asString(date).should.eql('2010-01-11T14:31:30.005');
28
- });
29
-
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
- };
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');
40
-
41
- tzDate = createFixedDate();
42
- tzDate.setMinutes((tzDate.getMinutes() - tzDate.getTimezoneOffset()) + 120);
43
- tzDate.getTimezoneOffset = function () {
44
- return 120;
45
- };
46
-
47
- dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
48
- .should.eql('2010-01-11T16:31:30.005-0200');
49
- });
50
-
51
- it('should provide a just-the-time format', function() {
52
- dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
53
- });
54
-
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
- };
61
-
62
- dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.16.11.01.10');
63
- });
64
- });
@@ -1,139 +0,0 @@
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() { dateFormat.parse() }).should.throw(/pattern must be supplied/);
9
- (function() { dateFormat.parse(null) }).should.throw(/pattern must be supplied/);
10
- (function() { dateFormat.parse('') }).should.throw(/pattern must be supplied/);
11
- });
12
-
13
- describe('with a pattern that has no replacements', function() {
14
- it('should return a new date when the string matches', function() {
15
- dateFormat.parse('cheese', 'cheese').should.be.a.Date()
16
- });
17
-
18
- it('should throw if the string does not match', function() {
19
- (function() {
20
- dateFormat.parse('cheese', 'biscuits');
21
- }).should.throw(/String 'biscuits' could not be parsed as 'cheese'/);
22
- });
23
- });
24
-
25
- describe('with a full pattern', function() {
26
- var pattern = 'yyyy-MM-dd hh:mm:ss.SSSO';
27
-
28
- it('should return the correct date if the string matches', function() {
29
- var testDate = new Date();
30
- testDate.setFullYear(2018);
31
- testDate.setMonth(8);
32
- testDate.setDate(13);
33
- testDate.setHours(18);
34
- testDate.setMinutes(10);
35
- testDate.setSeconds(12);
36
- testDate.setMilliseconds(392);
37
- testDate.getTimezoneOffset = function() { return 600; };
38
-
39
- dateFormat.parse(pattern, '2018-09-13 08:10:12.392+1000').getTime().should.eql(testDate.getTime());
40
- });
41
-
42
- it('should throw if the string does not match', function() {
43
- (function() {
44
- dateFormat.parse(pattern, 'biscuits')
45
- }).should.throw(/String 'biscuits' could not be parsed as 'yyyy-MM-dd hh:mm:ss.SSSO'/);
46
- });
47
- });
48
-
49
- describe('with a partial pattern', function() {
50
- var testDate = new Date();
51
- dateFormat.now = function() { return testDate; };
52
-
53
- function verifyDate(actual, expected) {
54
- actual.getFullYear().should.eql(expected.year || testDate.getFullYear());
55
- actual.getMonth().should.eql(expected.month || testDate.getMonth());
56
- actual.getDate().should.eql(expected.day || testDate.getDate());
57
- actual.getHours().should.eql(expected.hours || testDate.getHours());
58
- actual.getMinutes().should.eql(expected.minutes || testDate.getMinutes());
59
- actual.getSeconds().should.eql(expected.seconds || testDate.getSeconds());
60
- actual.getMilliseconds().should.eql(expected.milliseconds || testDate.getMilliseconds());
61
- }
62
-
63
- it('should return a date with missing values defaulting to current time', function() {
64
- var date = dateFormat.parse('yyyy-MM', '2015-09');
65
- verifyDate(date, { year: 2015, month: 8 });
66
- });
67
-
68
- it('should handle variations on the same pattern', function() {
69
- var date = dateFormat.parse('MM-yyyy', '09-2015');
70
- verifyDate(date, { year: 2015, month: 8 });
71
-
72
- date = dateFormat.parse('yyyy MM', '2015 09');
73
- verifyDate(date, { year: 2015, month: 8 });
74
-
75
- date = dateFormat.parse('MM, yyyy.', '09, 2015.');
76
- verifyDate(date, { year: 2015, month: 8 });
77
- });
78
-
79
- it('should match all the date parts', function() {
80
- var date = dateFormat.parse('dd', '21');
81
- verifyDate(date, { day: 21 });
82
-
83
- date = dateFormat.parse('hh', '12');
84
- verifyDate(date, { hours: 12 });
85
-
86
- date = dateFormat.parse('mm', '34');
87
- verifyDate(date, { minutes: 34 });
88
-
89
- date = dateFormat.parse('ss', '59');
90
- verifyDate(date, { seconds: 59 });
91
-
92
- date = dateFormat.parse('ss.SSS', '23.452');
93
- verifyDate(date, { seconds: 23, milliseconds: 452 });
94
-
95
- date = dateFormat.parse('hh:mm O', '05:23 +1000');
96
- verifyDate(date, { hours: 15, minutes: 23 });
97
-
98
- date = dateFormat.parse('hh:mm O', '05:23 -200');
99
- verifyDate(date, { hours: 3, minutes: 23 });
100
-
101
- date = dateFormat.parse('hh:mm O', '05:23 +0930');
102
- verifyDate(date, { hours: 14, minutes: 53 });
103
- });
104
- });
105
-
106
- describe('with a date formatted by this library', function() {
107
- var testDate = new Date();
108
- testDate.setUTCFullYear(2018);
109
- testDate.setUTCMonth(8);
110
- testDate.setUTCDate(13);
111
- testDate.setUTCHours(18);
112
- testDate.setUTCMinutes(10);
113
- testDate.setUTCSeconds(12);
114
- testDate.setUTCMilliseconds(392);
115
-
116
- it('should format and then parse back to the same date', function() {
117
- dateFormat.parse(
118
- dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
119
- dateFormat(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, testDate)
120
- ).should.eql(testDate);
121
-
122
- dateFormat.parse(
123
- dateFormat.ISO8601_FORMAT,
124
- dateFormat(dateFormat.ISO8601_FORMAT, testDate)
125
- ).should.eql(testDate);
126
-
127
- dateFormat.parse(
128
- dateFormat.DATETIME_FORMAT,
129
- dateFormat(dateFormat.DATETIME_FORMAT, testDate)
130
- ).should.eql(testDate);
131
-
132
- dateFormat.parse(
133
- dateFormat.ABSOLUTETIME_FORMAT,
134
- dateFormat(dateFormat.ABSOLUTETIME_FORMAT, testDate)
135
- ).should.eql(testDate);
136
- });
137
-
138
- });
139
- });