0

For example, if there is a date string, regardless of whether it has a time zone:2019-10-12T05:24:24.872043Z, how can I convert it into a date object for the corresponding time zone by using the time zone name Asia/Shanghai

Using only JavaScript and not relying on third-party libraries, I know that moment-timezone seems to solve this problem.

The following code can only get a string and cannot be converted to a date object in a given timezone:

var asiaTime = new Date('2019-10-09T08:26:24.289528Z').toLocaleString('en-US', {timeZone: "Asia/Shanghai"});

I expect to get a date object for a given timezone.

Scheinin
  • 195
  • 9
  • Date objects don't have a timezone, they're just a time value that is an offset from the epoch (1970-01-01T00:00:00Z), hence are UTC. They use host settings to deduce the local date and time. The *getTimezoneOffset* method returns the host system offset for the date and time represented by the Date's time value. – RobG Oct 12 '19 at 10:14
  • 1
    Probably a duplicate of [*Javascript set timezone in date object itself like setTimeOffset()*](https://stackoverflow.com/questions/44623363/javascript-set-timezone-in-date-object-itself-like-settimeoffset). – RobG Oct 12 '19 at 10:21

0 Answers0