Skip to content Skip to sidebar Skip to footer

Datetime From Utc In Local Time Format

I have a Date Time which is saved on the server in UTC format. The date is returned to the client looks like this: 2015-02-05T07:52:27.59 I need a javascript function which parses

Solution 1:

Split the string, zero reference the month, use Date.UTC when creating the new Date object. The output toString is local time unless you specify something else.

var dateTime = '2015-02-05T07:52:27.59',
    parts = dateTime.split(/[-T:\.]/g);

parts[1] -= 1;
document.body.textContent = newDate(Date.UTC.apply(null, parts)).toString();

Alternatively, if you have a modern browser. Append Z to the string and rely on the Date parse of the particular browser.

var dateTime = '2015-02-05T07:52:27.59Z';

document.body.textContent = newDate(dateTime).toString();

Solution 2:

Just append 'UTC' to the string:

var date = newDate('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"

Convert UTC date time to local date time using JavaScript

Post a Comment for "Datetime From Utc In Local Time Format"