Javascript – How to convert a UTC time to local time javascript


I have a requirement to convert a UTC time local time based on user timezone

I have two parameters utc time and users timezone as a string

0,1,2,3 …12 (timezone)
0,-1,-2,-3 …-12 (timezone)

var utc = "2014-10-18T06:14:41.512Z"
tz = 5.5(Indian Standard Time)

Expected result Sat Oct 18 2014 11:44:28 GMT+0530

I have tried moment js

moment("2014-10-18T06:14:41.512Z").zone('+05:30').format('YYYY-MM-DD HH:mm')

and the result is correct.

But when i change the timezone to other it is not showing as expected result

moment("2014-10-18T06:14:41.512Z").zone('+12:00').format('YYYY-MM-DD HH:mm')

result "2014-10-18 18:14" Expected 2014-10-18 19:18

12 is NewZeland timezone. Please help me to solve this issue. Thank you

Best Solution

Check this

var date = new Date('2014-10-19 17:00:34 UTC');

var timezone = "America/New_York";
var utcDate = "2014-10-19T10:31:59.0537721Z";

var localDate = moment.utc(utcDate).tz(timezone).format()

Also check