1

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?

Colin DeClue
  • 2,194
  • 3
  • 26
  • 47
  • timestamps in milliseconds, example: `1339511550650` – Esailija Jun 12 '12 at 14:32
  • How can I get that? I'm using NewtonSoft JSON converter on a datetime object. Can I make that give me milliseconds? – Colin DeClue Jun 12 '12 at 14:33
  • In javascript, you can get that with `+new Date()`. You can then reconstruct it by passing it as argument -> `new Date(x)`. What are you using in server? – Esailija Jun 12 '12 at 14:33
  • Well, I still need it from the date string coming from my server. +new Date() will just give me the milliseconds of right now. – Colin DeClue Jun 12 '12 at 14:40
  • Yeah, it depends what you are running in server. In PHP for example you can do `time() . "000"` – Esailija Jun 12 '12 at 14:42
  • Ah, I'm using .NET, and using Newtonsoft to do my JSON. By default, it's giving me ISO time. I'm trying to make it give me milliseconds, instead, but having some trouble finding converters. – Colin DeClue Jun 12 '12 at 14:46
  • Does this help http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa ? Btw it will just be a regular `Double`. Here's one from Jon Skeet: http://stackoverflow.com/a/7983514/995876 – Esailija Jun 12 '12 at 14:48
  • If the last character of the timestring is a 'z' or 'Z', all the browsers know it is UTC. Can you just add a 'Z', if there is not one already. – kennebec Jun 12 '12 at 14:57
  • @kennebec: That did it, thank you. If you post it as an answer, I can accept it. – Colin DeClue Jun 12 '12 at 15:06
  • @kennebec what do you mean by all browsers? it certainly doesn't work in ie8 even. – Esailija Jun 12 '12 at 15:16
  • It works in IE9, which is all I'm supporting. – Colin DeClue Jun 12 '12 at 19:08

3 Answers3

1

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);

emrox
  • 524
  • 3
  • 14
  • 1
    you have to decrement the month by one. Months starting at zero in JavaScript date objects. – ausi Jun 12 '12 at 14:52
1

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.

timrwood
  • 10,611
  • 5
  • 35
  • 42
  • Looks nice. I'll give this the accepted answer, mostly because I hate how unreadable regex is, and the guy who gave me what works for me (only works for IE9) didn't post it as an answer. I don't think we'll use moment, just because this is the extent of the date stuff we're doing, and I've got it working, now. – Colin DeClue Jun 12 '12 at 19:17
0

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

ausi
  • 7,253
  • 2
  • 31
  • 48
  • This wouldn't be ideal because I would have to parse the string myself. I was hoping not to have to do that. – Colin DeClue Jun 12 '12 at 14:42
  • here https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse you can find details for parsing date strings. But as described there: _Timezones in ISO dates are not yet supported_ – ausi Jun 12 '12 at 14:47
  • You have to use the RFC2822 / IETF date syntax to add timezone information. – ausi Jun 12 '12 at 14:49
  • This was ignoring daylight savings time, for some reason. – Colin DeClue Jun 12 '12 at 15:07