OLW: Formatting Localized Datetimes

Here is a #Javascript #One-liner for converting a datetime in to the local time zone (provided your date string specifies the time zone it came from).

var dateStr = new Date(string);
var hours = (dateStr.getHours()>12)?dateStr.getHours()-12:dateStr.getHours();
return ( dateStr.getMonth()+1 + “/” + dateStr.getDate() + “/” + dateStr.getFullYear() + ” ” + ((hours == 0) ? 12 : hours) + “:” + ((dateStr.getMinutes()<10) ? (“0“+dateStr.getMinutes()) : dateStr.getMinutes()) + ” ” + ((dateStr.getHours()>12) ? “PM” : “AM”);

Outside of a dog, a book is your best friend, and inside of a dog, it’s too dark to read.” – Groucho Marx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s