Jason Cheek
Posts: 0
Joined: Sun Jan 12, 2014 8:00 am

datetime string format

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.

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

datetime string format

Hi -

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

Jason Cheek
Posts: 0
Joined: Sun Jan 12, 2014 8:00 am

datetime string format

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?

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

datetime string format

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

Jason Cheek
Posts: 0
Joined: Sun Jan 12, 2014 8:00 am

datetime string format

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.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

datetime string format

Hi Jason,

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

Jason Cheek
Posts: 0
Joined: Sun Jan 12, 2014 8:00 am

datetime string format

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.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

datetime string format

Hi Jason,

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

Return to “Issues”