Have a datestring be interpreted the same across browsers

So, I have an ISO date time coming back from my server, such as "2012-06-11T18:05". This time is in UTC. When I pass that string to Chrome or IE, they give me the time as in local time (so, for CDT it gives 01:05 PM) (which is what I want). If I pass it to Firefox, it assumes it's already in local time, it reports 6:05 PM in local time. So, I figured I'd tack on that it's UTC time into the string, by doing "2012-06-11T18:05+0000", instead. This works great in Chrome and Firefox, but IE reports it as an invalid date.

What's a cross-browser, standards compliant way I can specify that a given datetime string represents UTC time?

Answers


If you are looking for a good date library, I wrote moment.js to address issues like this. It features auto ISO8601 parsing as well.

moment("2012-06-11T18:05"); // parse as local time
moment("2012-06-11T18:05+0000"); // parse as utc time

Check out the documentation at http://momentjs.com/docs/ for more examples of what the library can do.


You can parse the incoming Data by your own and use this to build a valid Date-Object.

Somethin like this:

// input (from somewhere)
var incomingDate = "2012-06-11T18:05";
// parse Input using Regexp
var parsedIncomingDate= incomingDate.match(/^(\d{4})\-(\d{2})\-(\d{2})T(\d{2}):(\d{2})$/);
// convert parsed UTC times into ms accoring to the users browser timezone
var getUTCms = Date.UTC(parsedIncomingDate[1],parsedIncomingDate[2], parsedIncomingDate[3], parsedIncomingDate[4], parsedIncomingDate[5]); 

// ms to Date Object
var dateObj = new Date(getUTCms);


You can use the Date.UTC Method like this:

var my_date = new Date(Date.UTC(2012, 5, 11, 18, 5, 0));

see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/UTC


Need Your Help

Setting arguments in a kernel in OpenCL causes error

c linux parallel-processing opencl gpgpu

I am a beginner in OpenCL and thus writing a simple program to double the elements of an array.

Ios aps device token gets deactivated when using trigger.io and urbanairship (because of Reload?)

apple-push-notifications trigger.io urbanairship.com

I have a trigger.io app that is using UrbanAirship for push notifications. Everything works fine on android. I also had push notifications working on ios. However, at some point push notification s...