diff --git a/ampersand-state.js b/ampersand-state.js index 79a742e..ea9109e 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -4,6 +4,7 @@ var BBEvents = require('backbone-events-standalone'); var KeyTree = require('key-tree-store'); var arrayNext = require('array-next'); var changeRE = /^change:/; +var timezoneRE = /Z|[-+]\d\d:\d\d/; function Base(attrs, options) { options || (options = {}); @@ -594,6 +595,11 @@ var dataTypes = { // If the newVal cant be parsed, then try parseInt first dateVal = new Date(parseInt(newVal, 10)).valueOf(); if (isNaN(dateVal)) throw TypeError; + } else if (isNaN(Number(newVal)) && !timezoneRE.test(newVal)) { + // Interpret ISO 8601 Date strings that do not specify a + // time zone in the local system time zone, as per ECMAScript 6: + // http://people.mozilla.org/~jorendorff/es6-draft.html#sec-date-time-string-format + dateVal += new Date().getTimezoneOffset() * 60 * 1000; } newVal = dateVal; newType = 'date'; diff --git a/test/full.js b/test/full.js index e6eaab2..579c155 100644 --- a/test/full.js +++ b/test/full.js @@ -1480,9 +1480,13 @@ test("#99 #101 - string dates can be parsed", function(t) { t.ok(isDate(model.today)); t.equal(model.today.toJSON(), isoString, 'date should accept a native date object'); + model.today = 0; + t.ok(isDate(model.today)); + t.equal(model.today.toJSON(), new Date(0).toJSON(), 'date should accept zero'); + model.today = '2014-11-13'; t.ok(isDate(model.today)); - t.equal(model.today.toJSON(), '2014-11-13T00:00:00.000Z', 'date should accept YYYY-MM-DD'); + t.equal(model.today.toJSON(), new Date(2014, 10, 13).toJSON(), 'date should accept YYYY-MM-DD'); model.today = '2014-11-13T21:01Z'; t.ok(isDate(model.today));