andrewplummer/Sugar — Issue #111: Date.fromJSON
Author: josher19
Created: 2012-02-28 06:13:23+00:00
State: open

Hi Andrew,

On some older browsers (including Webkit for Android), `Date.create` and `new Date` will return an `Invalid Date` for dates that have been converted to JSON:

var now = new Date();
var asJson = JSON.parse(JSON.stringify(now)); // "2012-02-28T05:29:16.431Z"
var json_date = now.toJSON(); // ditto
if (self.console) console.log(json_date === asJson); // true
var today = new Date( json_date ); // 'Invalid Date' for some older browsers, including Webkit.
var created = Date.create( json_date ); // Ditto

Proposed solution:

Date.fromJSON(dateString); // convert from JSON format (ISO8601_DateString) to a Javascript Date
Date.isInvalid(date); // return true if date is an 'Invalid Date'

and the unit tests would basically be:

Date.fromJSON( d.toJSON() ) == d // true for all valid Dates d
Date.fromJSON( dstr ).toJSON() == dstr // true for all valid JSON datestrings.

I've got some code which does this which I could clean up, add unit tests, and submit to Sugar.
https://gist.github.com/1930025

This may also be related to Issues #99 and #98