-3

localDate: "2020-10-13" localTime: "20:00:00" dateTime: "2020-10-14T01:00:00Z"

I have these three datas, how to find the timezone using these data.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575

1 Answers1

0

Assuming your inputs are exactly as given:

var localDate = "2020-10-13";
var localTime = "20:00:00";
var dateTime = "2020-10-14T01:00:00Z";

var offsetMillis = new Date(localDate + 'T' + localTime + 'Z') - new Date(dateTime);
var offsetHours = offsetMillis / 36e5;
console.log(offsetHours); // -5

The result is -5, which is 5 hours behind UTC. This works by creating two date objects, one that is UTC-based, and one that pretends to be UTC-based but is actually using the local values. Subtracting the two yields the offset.

Keep in mind - this is an offset from UTC, not a time zone. One cannot determine the time zone from the offset alone. See "Time Zone != Offset" in the timezone tag wiki for more details.

Also keep in mind that not all time zone offsets will be in whole hours. For example, India uses UTC-5:30, so expect -5.5 from this code in such a case. (There also exist time zones with 45-minute offsets.)

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575