0

I'm trying to convert UTC time to local time, but the below code is not working. What's wrong in it?

var parsedStartDateTime = 
             new Date(moment.unix(parseInt(data['StartDateTime'].substr(6)) / 1000));
var startDateTimeMoment = 
             moment.tz(parsedStartDateTime, tzName);
var formatted_date = 
             startDateTimeMoment.format("MMM DD YYYY h:mm:ss A");
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
user2320213
  • 3
  • 1
  • 9
  • possible duplicate of [Convert UTC date time to local date time using JavaScript](http://stackoverflow.com/questions/6525538/convert-utc-date-time-to-local-date-time-using-javascript) – 0x9BD0 Nov 20 '14 at 18:57
  • What javascript library are you using there? (for moment.unix, moment.tz)? and What is the value of 'StartDateTime'? – MatthewMartin Nov 20 '14 at 18:57

3 Answers3

0

Try appending UTC to the string before converting it to a date then use toString() method of date.

Example:

var myDate = new Date('7/1/2014 5:22:55 PM UTC');
date.toString(); //this should give you local date and time

This code was taken from here

Community
  • 1
  • 1
brso05
  • 13,142
  • 2
  • 21
  • 40
0

To format your date try this:

var d = new Date();
var formatD = d.toLocaleFormat("%d.%m.%Y %H:%M (%a)");

Reference: Javascript to convert UTC to local time

Community
  • 1
  • 1
LostLife
  • 152
  • 8
0

Here is my solution:

function convertUTCDateToLocalDate(date) {
var newDate = new Date(date.getTime()+date.getTimezoneOffset()*60*1000);

var offset = date.getTimezoneOffset() / 60;
var hours = date.getHours();

newDate.setHours(hours - offset);

return newDate;   

}

var date = convertUTCDateToLocalDate(new Date(date_string_you_received));
date.toLocaleString().replace(/GMT.*/g,"");
Paradise
  • 558
  • 6
  • 17