1 /* global describe, it, xit, expect, beforeEach, jasmine */
3 describe('Date', function () {
6 var supportsDescriptors = Object.defineProperty && (function () {
9 Object.defineProperty(obj, 'x', {
13 for (var _ in obj) { // jscs:ignore disallowUnusedVariables
17 } catch (e) { /* this is ES3 */
21 var ifSupportsDescriptorsIt = supportsDescriptors ? it : xit;
22 var has = Object.prototype.hasOwnProperty;
25 beforeEach(function () {
26 var negativeCanned = [
28 getTime: -3509827329600292,
34 getTime: -3509824651200292,
40 getTime: -3509822145600292,
46 getTime: -3509819467200292,
52 getTime: -3509816875200292,
58 getTime: -3509814196800292,
64 getTime: -3509811604800292,
70 getTime: -3509808926400292,
76 getTime: -3509806248000292,
82 getTime: -3509803656000292,
88 getTime: -3509800977600292,
94 getTime: -3509798385600292,
100 negativeDate = negativeCanned.map(function (item) {
101 var dateFirst = new Date(item.getTime);
102 var dateLast = new Date(item.getTime + ((item.dim - 1) * 86400000));
104 dates: [dateFirst, dateLast],
106 getUTCDay: [item.getUTCDay, (item.getUTCDay + item.dim - 1) % 7],
107 getDay: [item.getDay, (item.getDay + item.dim - 1) % 7]
112 describe('.now()', function () {
113 it('should be the current time', function () {
114 var before = (new Date()).getTime();
115 var middle = Date.now();
116 var after = (new Date()).getTime();
117 expect(middle).not.toBeLessThan(before);
118 expect(middle).not.toBeGreaterThan(after);
122 describe('constructor', function () {
123 it('works with standard formats', function () {
124 // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1
125 expect(+new Date('2012-12-31T23:59:59.000Z')).toBe(1356998399000); // 1356998399000 1356998399000 1356998399000 1356998399000 1356998399000
126 expect(+new Date('2012-04-04T05:02:02.170Z')).toBe(1333515722170); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
127 expect(+new Date('2012-04-04T05:02:02.170999Z')).toBe(1333515722170); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
128 expect(+new Date('2012-04-04T05:02:02.17Z')).toBe(1333515722170); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
129 expect(+new Date('2012-04-04T05:02:02.1Z')).toBe(1333515722100); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
130 expect(+new Date('2012-04-04T24:00:00.000Z')).toBe(1333584000000); // NaN 1333584000000 1333584000000 1333584000000 1333584000000
131 expect(+new Date('2012-02-29T12:00:00.000Z')).toBe(1330516800000); // 1330516800000 1330516800000 1330516800000 1330516800000 1330516800000
132 expect(+new Date('2011-03-01T12:00:00.000Z')).toBe(1298980800000); // 1298980800000 1298980800000 1298980800000 1298980800000 1298980800000
134 // https://github.com/es-shims/es5-shim/issues/80 Safari bug with leap day
135 expect(new Date('2034-03-01T00:00:00.000Z') -
136 new Date('2034-02-27T23:59:59.999Z')).toBe(86400001); // 86400001 86400001 86400001 86400001 1
140 ifSupportsDescriptorsIt('is not enumerable', function () {
141 expect(Object.keys(new Date())).not.toContain('constructor');
144 it('works as a function', function () {
145 var zeroDate = Date(0);
146 expect(zeroDate).toBe(String(zeroDate));
147 var value = Date(1441705534578);
148 expect(value).toBe(String(value));
151 it('fixes this Safari 8/9 bug', function () {
152 var offset = new Date(1970).getTimezoneOffset() * 60e3;
154 var timestamp = 2147483647; // Math.pow(2, 31) - 1
155 var date = new Date(1970, 0, 1, 0, 0, 0, timestamp);
156 var expectedTimestamp = timestamp + offset;
157 expect(date.getTime()).toBe(expectedTimestamp);
159 var brokenTimestamp = 2147483648; // Math.pow(2, 31)
160 var brokenDate = new Date(1970, 0, 1, 0, 0, 0, brokenTimestamp);
161 var expectedBrokenTimestamp = brokenTimestamp + offset;
162 expect(brokenDate.getTime()).toBe(expectedBrokenTimestamp); // NaN in Safari 8/9
164 var veryBrokenTS = 1435734000000;
165 var veryBrokenDate = new Date(1970, 0, 1, 0, 0, 0, veryBrokenTS);
166 var largeDate = new Date('Wed Jul 01 2015 07:00:00 GMT-0700 (PDT)');
167 var expectedVeryBrokenTS = veryBrokenTS + (largeDate.getTimezoneOffset() * 60e3);
168 expect(veryBrokenDate.getTime()).toBe(expectedVeryBrokenTS); // NaN in Safari 8/9
171 it('works with a Date object', function () {
172 var date = new Date(1456297712984);
173 var copiedDate = new Date(date);
174 expect(date).not.toBe(copiedDate);
175 expect(copiedDate.getTime()).toBe(date.getTime());
176 expect(+copiedDate).toBe(+date);
177 expect(String(copiedDate)).toBe(String(date));
181 describe('.parse()', function () {
182 // TODO: Write the rest of the test.
184 ifSupportsDescriptorsIt('is not enumerable', function () {
185 expect(Object.getOwnPropertyDescriptor(Date, 'parse').enumerable).toBe(false);
188 it('should be an invalid date', function () {
189 // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1
190 expect(Date.parse('2012-11-31T23:59:59.000Z')).toBeFalsy(); // 1354406399000 NaN NaN 1354406399000 NaN
191 expect(Date.parse('2012-12-31T23:59:60.000Z')).toBeFalsy(); // NaN NaN NaN NaN 1356998400000
192 expect(Date.parse('2012-04-04T24:00:00.500Z')).toBeFalsy(); // NaN NaN 1333584000500 1333584000500 NaN
193 expect(Date.parse('2012-12-31T10:08:60.000Z')).toBeFalsy(); // NaN NaN NaN NaN 1356948540000
194 expect(Date.parse('2012-13-01T12:00:00.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
195 expect(Date.parse('2012-12-32T12:00:00.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
196 expect(Date.parse('2012-12-31T25:00:00.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
197 expect(Date.parse('2012-12-31T24:01:00.000Z')).toBeFalsy(); // NaN NaN NaN 1356998460000 NaN
198 expect(Date.parse('2012-12-31T12:60:00.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
199 expect(Date.parse('2012-12-31T12:00:60.000Z')).toBeFalsy(); // NaN NaN NaN NaN 1356955260000
200 expect(Date.parse('2012-00-31T23:59:59.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
201 expect(Date.parse('2012-12-00T23:59:59.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
202 expect(Date.parse('2011-02-29T12:00:00.000Z')).toBeFalsy(); // 1298980800000 NaN NaN 1298980800000 NaN
205 it('should work', function () {
207 // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 Safari 8
208 '2012-12-31T23:59:59.000Z': 1356998399000, // 1356998399000 1356998399000 1356998399000 1356998399000 1356998399000 1356998399000
209 '2012-04-04T05:02:02.170Z': 1333515722170, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
210 '2012-04-04T05:02:02.170999Z': 1333515722170, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170.999
211 '2012-04-04T05:02:02.17Z': 1333515722170, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
212 '2012-04-04T05:02:02.1Z': 1333515722100, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
213 '2012-04-04T24:00:00.000Z': 1333584000000, // NaN 1333584000000 1333584000000 1333584000000 1333584000000 1333584000000
214 '2012-02-29T12:00:00.000Z': 1330516800000, // 1330516800000 1330516800000 1330516800000 1330516800000 1330516800000 1330516800000
215 '2011-03-01T12:00:00.000Z': 1298980800000 // 1298980800000 1298980800000 1298980800000 1298980800000 1298980800000 1298980800000
217 for (var str in dates) {
218 if (has.call(dates, str)) {
219 expect(Math.floor(Date.parse(str))).toBe(dates[str]);
223 // https://github.com/es-shims/es5-shim/issues/80 Safari bug with leap day
224 expect(Date.parse('2034-03-01T00:00:00.000Z') -
225 Date.parse('2034-02-27T23:59:59.999Z')).toBe(86400001); // 86400001 86400001 86400001 86400001 1
229 it('fixes a Safari 8/9 bug with parsing in UTC instead of local time', function () {
230 var offset = new Date('2015-07-01').getTimezoneOffset() * 60e3;
231 expect(Date.parse('2015-07-01T00:00:00')).toBe(1435708800000 + offset); // Safari 8/9 give NaN
234 it('should support extended years', function () {
235 // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1
236 expect(Date.parse('0000-01-01T00:00:00.000Z')).toBe(-621672192e5); // -621672192e5 -621672192e5 -621672192e5 -621672192e5 -621672192e5
237 expect(Date.parse('0001-01-01T00:00:00Z')).toBe(-621355968e5); // -621355968e5 -621355968e5 -621355968e5 8.64e15 -621355968e5
238 expect(Date.parse('+275760-09-13T00:00:00.000Z')).toBe(8.64e15); // 8.64e15 NaN 8.64e15 8.64e15 8.64e15
239 expect(Date.parse('-271821-04-20T00:00:00.000Z')).toBe(-8.64e15); // -8.64e15 NaN -8.64e15 -8.64e15 -8.6400000864e15
240 expect(Date.parse('+275760-09-13T00:00:00.001Z')).toBeFalsy(); // NaN NaN NaN 8.64e15 + 1 8.64e15 + 1
241 expect(Date.parse('-271821-04-19T23:59:59.999Z')).toBeFalsy(); // NaN NaN NaN -8.64e15 - 1 -8.6400000864e15 - 1
242 expect(Date.parse('+033658-09-27T01:46:40.000Z')).toBe(1e15); // 1e15 NaN 1e15 1e15 9999999136e5
243 expect(Date.parse('-000001-01-01T00:00:00Z')).toBe(-621987552e5); // -621987552e5 NaN -621987552e5 -621987552e5 -621987552e5
244 expect(Date.parse('+002009-12-15T00:00:00Z')).toBe(12608352e5); // 12608352e5 NaN 12608352e5 12608352e5 12608352e5
247 it('works with timezone offsets', function () {
248 // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1
249 expect(Date.parse('2012-01-29T12:00:00.000+01:00')).toBe(132783480e4); // 132783480e4 132783480e4 132783480e4 132783480e4 132783480e4
250 expect(Date.parse('2012-01-29T12:00:00.000-00:00')).toBe(132783840e4); // 132783840e4 132783840e4 132783840e4 132783840e4 132783840e4
251 expect(Date.parse('2012-01-29T12:00:00.000+00:00')).toBe(132783840e4); // 132783840e4 132783840e4 132783840e4 132783840e4 132783840e4
252 expect(Date.parse('2012-01-29T12:00:00.000+23:59')).toBe(132775206e4); // 132775206e4 132775206e4 132775206e4 132775206e4 132775206e4
253 expect(Date.parse('2012-01-29T12:00:00.000-23:59')).toBe(132792474e4); // 132792474e4 132792474e4 132792474e4 132792474e4 132792474e4
254 expect(Date.parse('2012-01-29T12:00:00.000+24:00')).toBeFalsy(); // NaN 1327752e6 NaN 1327752000000 1327752000000
255 expect(Date.parse('2012-01-29T12:00:00.000+24:01')).toBeFalsy(); // NaN NaN NaN 1327751940000 1327751940000
256 expect(Date.parse('2012-01-29T12:00:00.000+24:59')).toBeFalsy(); // NaN NaN NaN 1327748460000 1327748460000
257 expect(Date.parse('2012-01-29T12:00:00.000+25:00')).toBeFalsy(); // NaN NaN NaN NaN NaN
258 expect(Date.parse('2012-01-29T12:00:00.000+00:60')).toBeFalsy(); // NaN NaN NaN NaN NaN
259 expect(Date.parse('-271821-04-20T00:00:00.000+00:01')).toBeFalsy(); // NaN NaN NaN -864000000006e4 -864000008646e4
260 expect(Date.parse('-271821-04-20T00:01:00.000+00:01')).toBe(-8.64e15); // -8.64e15 NaN -8.64e15 -8.64e15 -864000008640e4
262 // When time zone is missed, local offset should be used (ES 5.1 bug)
263 // see https://bugs.ecmascript.org/show_bug.cgi?id=112
264 var tzOffset = Number(new Date(1970, 0));
265 // same as (new Date().getTimezoneOffset() * 60000)
266 expect(Date.parse('1970-01-01T00:00:00')).toBe(tzOffset); // tzOffset 0 0 0 NaN
269 it('should be able to coerce to a number', function () {
270 var actual = Number(new Date(1970, 0));
271 var expected = parseInt(actual, 10);
272 expect(actual).toBeDefined();
273 expect(actual).toBe(expected);
274 expect(isNaN(actual)).toBeFalsy();
279 describe('#toString()', function () {
282 beforeEach(function () {
283 actual = (new Date(1970, 0)).toString();
286 it('should show correct date info for ' + actual, function () {
287 expect(actual).toMatch(/1970/);
288 expect(actual).toMatch(/jan/i);
289 expect(actual).toMatch(/thu/i);
290 expect(actual).toMatch(/00:00:00/);
294 describe('#valueOf()', function () {
295 // Note that new Date(1970, 0).valueOf() is 0 in UTC timezone.
296 // Check check that it's a number (and an int), not that it's "truthy".
299 beforeEach(function () {
300 actual = (new Date(1970, 0)).valueOf();
302 it('should give a numeric value', function () {
303 expect(typeof actual).toBe('number');
305 it('should not be NaN', function () {
306 expect(isNaN(actual)).toBe(false);
308 it('should give an int value', function () {
309 expect(actual).toBe(Math.floor(actual));
313 describe('#getUTCDate()', function () {
314 it('should return the right value for negative dates', function () {
315 // Opera 10.6/11.61/Opera 12 bug
316 negativeDate.forEach(function (item) {
317 item.dates.forEach(function (date, index) {
318 expect(date.getUTCDate()).toBe(item.days[index], date);
324 describe('#getUTCDay()', function () {
325 it('should return the right value for negative dates', function () {
326 negativeDate.forEach(function (item) {
327 item.dates.forEach(function (date, index) {
328 expect(date.getUTCDay()).toBe(item.getUTCDay[index]);
334 describe('#getUTCFullYear()', function () {
335 it('should return the right value for negative dates', function () {
336 // Opera 10.6/11.61/Opera 12 bug
337 negativeDate.forEach(function (item) {
338 item.dates.forEach(function (date) {
339 expect(date.getUTCFullYear()).toBe(-109252);
345 describe('#getUTCMonth()', function () {
346 it('should return the right value for negative dates', function () {
347 // Opera 10.6/11.61/Opera 12 bug
348 negativeDate.forEach(function (item, index) {
349 item.dates.forEach(function (date) {
350 expect(date.getUTCMonth()).toBe(index);
355 it('should return correct values', function () {
356 expect(new Date(8.64e15).getUTCMonth()).toBe(8);
357 expect(new Date(0).getUTCMonth()).toBe(0);
361 describe('#getUTCHours()', function () {
362 it('should return the right value for negative dates', function () {
363 negativeDate.forEach(function (item) {
364 item.dates.forEach(function (date) {
365 expect(date.getUTCHours()).toBe(11);
371 describe('#getUTCMinutes()', function () {
372 it('should return the right value for negative dates', function () {
373 negativeDate.forEach(function (item) {
374 item.dates.forEach(function (date) {
375 expect(date.getUTCMinutes()).toBe(59);
381 describe('#getUTCSeconds()', function () {
382 it('should return the right value for negative dates', function () {
383 negativeDate.forEach(function (item) {
384 item.dates.forEach(function (date) {
385 expect(date.getUTCSeconds()).toBe(59);
391 describe('#getUTCMilliseconds()', function () {
392 it('should return the right value for negative dates', function () {
393 // Opera 10.6/11.61/Opera 12 bug
394 negativeDate.forEach(function (item) {
395 item.dates.forEach(function (date) {
396 expect(date.getUTCMilliseconds()).toBe(708);
402 describe('#getDate()', function () {
403 it('should return the right value for negative dates', function () {
404 negativeDate.forEach(function (item) {
405 item.dates.forEach(function (date, index) {
406 expect(date.getDate()).toBe(item.days[index]);
412 describe('#getDay()', function () {
413 it('should return the right value for negative dates', function () {
414 negativeDate.forEach(function (item) {
415 item.dates.forEach(function (date, index) {
416 expect(date.getDay()).toBe(item.getDay[index]);
422 describe('#getFullYear()', function () {
423 it('should return the right value for negative dates', function () {
424 // Opera 10.6/11.61/Opera 12 bug
425 negativeDate.forEach(function (item) {
426 item.dates.forEach(function (date) {
427 expect(date.getFullYear()).toBe(-109252);
433 describe('#getMonth()', function () {
434 it('should return the right value for negative dates', function () {
435 // Opera 10.6/11.61/Opera 12 bug
436 negativeDate.forEach(function (item, index) {
437 item.dates.forEach(function (date) {
438 expect(date.getMonth()).toBe(index);
444 describe('#getHours()', function () {
445 it('should return the right value for negative dates', function () {
446 negativeDate.forEach(function (item) {
447 item.dates.forEach(function (date) {
448 expect(date.getHours() + Math.floor(date.getTimezoneOffset() / 60)).toBe(11);
454 describe('#getMinutes()', function () {
455 it('should return the right value for negative dates', function () {
456 negativeDate.forEach(function (item) {
457 item.dates.forEach(function (date) {
458 var off = date.getTimezoneOffset();
459 var offHours = Math.floor(off / 60);
460 var offMins = off - offHours * 60;
461 expect(date.getMinutes() + offMins).toBe(59);
467 describe('#getSeconds()', function () {
468 it('should return the right value for negative dates', function () {
469 negativeDate.forEach(function (item) {
470 item.dates.forEach(function (date) {
471 expect(date.getSeconds()).toBe(59);
477 describe('#getMilliseconds()', function () {
478 it('should return the right value for negative dates', function () {
479 // Opera 10.6/11.61/Opera 12 bug
480 negativeDate.forEach(function (item) {
481 item.dates.forEach(function (date) {
482 expect(date.getMilliseconds()).toBe(708);
488 describe('#toISOString()', function () {
489 // TODO: write the rest of the test.
491 it('should support extended years', function () {
492 expect(new Date(-62198755200000).toISOString().indexOf('-000001-01-01')).toBe(0);
493 expect(new Date(8.64e15).toISOString().indexOf('+275760-09-13')).toBe(0);
496 it('should return correct dates', function () {
497 expect(new Date(-1).toISOString()).toBe('1969-12-31T23:59:59.999Z'); // Safari 5.1.5 "1969-12-31T23:59:59.-01Z"
498 negativeDate.forEach(function (item, index) {
500 item.dates.forEach(function (date, idx) {
501 var d = item.days[idx];
502 expect(date.toISOString()).toBe('-109252-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + 'T11:59:59.708Z'); // Opera 11.61/Opera 12 bug with Date#getUTCMonth
509 describe('#toUTCString()', function () {
510 it('should return correct dates', function () {
511 expect(new Date(-1509842289600292).toUTCString()).toBe('Mon, 01 Jan -45875 11:59:59 GMT');
515 describe('#toDateString()', function () {
516 it('should return correct dates', function () {
517 expect(new Date(-1509842289600292).toDateString()).toBe('Mon Jan 01 -45875');
521 describe('#toString()', function () {
522 it('should return correct dates', function () {
523 var actual = new Date(1449662400000).toString();
524 var re = /^Wed Dec 09 2015 \d\d:\d\d:\d\d GMT[-\+]\d\d\d\d(?: |$)/;
525 expect(re.test(actual)).toBe(true, actual);
529 describe('#toJSON()', function () {
531 // Opera 11.6x/12 bug
532 it('should call toISOString', function () {
533 var date = new Date(0);
534 date.toISOString = function () {
537 expect(date.toJSON()).toBe(1);
540 it('should return null for not finite dates', function () {
541 var date = new Date(NaN),
544 json = date.toJSON();
547 expect(e).not.toEqual(jasmine.any(Error));
549 expect(json).toBe(null);
552 it('should return the isoString when stringified', function () {
553 var date = new Date();
554 expect(JSON.stringify(date.toISOString())).toBe(JSON.stringify(date));