Page 1 of 1

datetime string format

Posted: Sun Feb 02, 2014 2:20 am
by Jason Cheek

Why does the datetime string returned from the database not have the 'T' and 'Z' as specified in the standard? Is there a way to change this behavior or to make the javascript Date object accept this string format? It seems wasteful to reformat every datetime string from the database just to make them parse-able by the Date object.


datetime string format

Posted: Sun Feb 02, 2014 3:14 am
by Illya Stepanov

Hi -

The MongoDB stores data in a specific format (ISODate object), but service returns data in JSON-object which is represented as string values.


datetime string format

Posted: Sun Feb 02, 2014 3:21 am
by Jason Cheek

Oh, I have been using it as a datetime string in passing it directly to the Date object. For example:
var d = new Date(value);
return d.getMinutes();

Is there a better way to do this?


datetime string format

Posted: Sun Feb 02, 2014 12:06 pm
by Igor

You can use this code in mappings to convert date into ISO string:
code
var d = new Date(value);
return d.toISOString();
/code


datetime string format

Posted: Sun Feb 02, 2014 7:11 pm
by Jason Cheek

That's my problem. Date is rejecting the string as invalid. I have to manually parse the string and add the 'T' and 'Z' to get Date to accept the string.


datetime string format

Posted: Mon Feb 03, 2014 2:19 pm
by Kateryna Grynko

Hi Jason,

You don't need symbols T and Z to create a Date object . Do you use Database data to create JS objects?


datetime string format

Posted: Mon Feb 03, 2014 7:00 pm
by Jason Cheek

If I pass the unaltered string returned by the database to the Date constructor, all calls to the Date functions such as getHours() and getDate() return NaN. The Date constructor only parses the datetime string correctly if I add the T and Z into the string. This has been true for both Firefox and IE in both Windows 7 and Windows 8.1. This behavior has persisted through 3 separate apps.

In every test it has not worked unless I modify the database string like this:

function correctApperyDateStringFormat(str) {
var dt = str.split(' ');
return dt[0] + 'T' + dt[1] + 'Z';
}

So I must create all Date objects from database datetime strings like this:

var dt = new Date(correctApperyDateStringFormat(value));

Or else the Date object will be invalid.


datetime string format

Posted: Tue Feb 04, 2014 2:07 pm
by Kateryna Grynko

Hi Jason,

Could you please try in Chrome? Webkit browsers work without it.