4

I have a UTC date time string and time zone name. I am getting this from a third party API. I need to convert the UTC time to local time of that timezone using JavaScript/NodeJS as well as get that time zone's current time. Is there any library/method available for the same?

var timezone = "America/New_York";//This will always be in Olson format.
var UTCTime = "2017-09-03T02:00:00Z";
var localTime; //I need to find the equivalent of UTCTime for America/New_York
var currentTime; //Current time of time zone America/New_York
user1640256
  • 1,691
  • 7
  • 25
  • 48
  • Helpful resource https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date – atiq1589 Aug 08 '17 at 10:23
  • moment (http://momentjs.com/docs/) is a very powerful and popular Date manipulation library, most likely your requirement can be covered by it. – tibetty Aug 08 '17 at 10:24

2 Answers2

6

This can be done in few ways:

var options = {
    timeZone: "America/New_York",
    year: 'numeric', month: 'numeric', day: 'numeric',
    hour: 'numeric', minute: 'numeric', second: 'numeric'
};

var formatter = new Intl.DateTimeFormat([], options);

var UTCTime = "2017-09-03T02:00:00Z";
var localTime = formatter.format(new Date(UTCTime));
var currentTime = formatter.format(new Date()); console.log(currentTime, localTime);

Or you can use moment.js library.

You can use Intl.DateTimeFormat in the most modern browsers. Intl.DateTimeFormat has been supported by most modern browsers.

asmmahmud
  • 4,844
  • 2
  • 40
  • 47
1

You could use getTimezoneOffset() which returns the offset in minutes. You can then modify your date to the corresponding return of the function.

You may also want to look at moment.js which is a very useful library when it comes to javascipt dates.

Sean
  • 1,444
  • 1
  • 11
  • 21