Skip to content Skip to sidebar Skip to footer

How Can I Convert Datetime Microformat To Local Time In Javascript?

I have a page that is currently using the datetime microformat to display a timestamp, but I have only been showing the human-readable time for my own time zone: Copy

Unfortunately, it doesn't handle timezone abbreviations either. You would have to modify the "tzre" expression to accept letters, and the only solution I know of to deal with timezone abbreviations in Javascript is to have a look-up table which you keep up to date manually in the event of changes to regional daylight savings times.

Solution 2:

EcmaScript formalized the addition of an ISO-8601 style string as an imput for a JavaScript date. Since most JS implementations don't support this, I created a wrapper to the Date object, that has this functionality. If you set the title tags to output in UTC/GMT/Z/Zulu offset, you can use my EcmaScript 5 extensions for JS's Date object.

For DateTime values that are to be used in client-side scripts, I generally try to always do the following. Store date+time in UTC zone (even in databases). Transmit date-times in UTC zone. From client to server, you can use the .toISOString() method in the above link. From server-to client this is relatively easy.

Via jQuery (with extension):

$('.published').each(function(){
  var dtm = newDate(this.title);
  if (!isNaN(dtm)) {
    this.text(dtm.toString());
  }
});

I don't recall if I added support for non-utc date-times in the input, but wouldn't be too hard to account for them.

Post a Comment for "How Can I Convert Datetime Microformat To Local Time In Javascript?"