How to convert a UTC time to local time javascript

13,358

Solution 1

Check this

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


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

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

Also check

http://www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/

Solution 2

To adhere to international standards, you need to format your UTC date to include the time delimiter T, and the zone designator Z.

Z is the timezone designator for the zero UTC offset aka Zulu time. You can read more about the International Date Standard ISO8601 format specifics here.

Once you've conformed to the international standard, the cross browser friendly approach is simple:

new Date('2014-10-19T17:00:34Z');
// Sun Oct 19 2014 12:00:34 GMT-0500 (Central Daylight Time)

Solution 3

A time zone is not an offset. An offset is only part of a time zone. Many time zones alternate between two different offsets to account for daylight saving time. The time zone has to account for this, including the specific dates and times that daylight saving time begins and ends, as well as any history of changes that the time zone may have had.

The New Zealand case you gave is a perfect example. You said "12 is New Zealand timezone", and thus expected since New Zealand is in DST for that date that the conversion from 6:14 UTC to New Zealand local time would be 19:14. - 13 hours later.

But 12 doesn't fully represent New Zealand. It is just a 12 hour offset from UTC. There are plenty of other time zones that use the same offset in different ways. For example, the Marshal Islands use UTC+12 year round, without daylight saving time.

You should really read the timezone tag wiki - especially the section titled "Time Zone != Offset".

Instead of offsets, you should represent time zones with their full IANA identifier from the tz database. For example US Eastern Time is "America/New_York", Indian Time is "Asia/Kolkata", and New Zealand Time is "Pacific/Auckland". You can find more in the list on Wikipedia.

You can use moment-timezone to work with these in JavaScript.

moment("2014-10-18T06:14:41.512Z").tz('Pacific/Auckland').format('YYYY-MM-DD HH:mm')

//  Output: "2014-10-18 19:14"

I also cover these topics in great detail in my Date and Time Fundamentals course on Pluralsight.com.

Share:
13,358
Dibish
Author by

Dibish

Software Engineer, AMT

Updated on July 20, 2022

Comments

  • Dibish
    Dibish over 1 year

    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

    ie 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 tried

    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

  • Dibish
    Dibish over 9 years
    thank you, but i am doing this in server side. i have users timezone in my hand, is there any way to convert it in server side using moment.js
  • Dibish
    Dibish over 9 years
    Thanks, but i dont know the zone ie "America/New_York". I have only zone number like 5.5 for +05:30
  • Satish Shinde
    Satish Shinde over 9 years
    You can use UTC+05:30 as timezone
  • Josh Wulf
    Josh Wulf about 8 years
    All good points. Timezones are more than offsets. Brisbane and Sydney are at the same longitude, but Sydney has DST and Brisbane does not. So we stay +10 all year round, they oscillate between +10 and +11.
  • Varshini
    Varshini over 6 years
    Can we convert UTC timezone such as EST to IANA format America/New_York?