0

I have some HTML rendered by a Django template that formats a timestamp in ISO 8601 format, e.g:

<span class="my-date">2015-06-04T13:00:00</span>

I am using some Javascript to convert this timestamp to the user's local timezone:

$(document).ready(function(){
  $('.my-date').each( function(){
    var tz_date = new Date( $(this).text() );
    $(this).text( tz_date.toString() );
  });
});

This works fine in Chrome (e.g, results in Thu Jun 04 2015 09:00:00 GMT-0400 (EDT)), but I'm having an issue in Firefox, which is showing Thu Jun 04 2015 13:00:00 GMT-0400 (EDT). It seems Firefox is correctly appending the user's timezone, but it is not adjusting the hours.

I wanted to check here if there's some poor assumption I'm making in my code, or if this is a bug in Firefox.

jsFiddle

Joseph
  • 12,678
  • 19
  • 76
  • 115
  • 1
    possible duplicate of [new Date() works differently in Chrome and Firefox](http://stackoverflow.com/questions/15109894/new-date-works-differently-in-chrome-and-firefox) – Brandon Taylor Jun 04 '15 at 15:34

1 Answers1

0

Rather than use ISO 8601 timestamps, you can use RFC 2822 timestamps. Both Django and javascript's Date.parse() are compatible with this, and both Chrome's and Firefox's implementation work the same way with it.

The only change that needs to be made is in the Django template that renders the timestamp server side. Rather than use |date:'c', simply use |date:'r'.

Joseph
  • 12,678
  • 19
  • 76
  • 115