Get timezone from date string in JavaScript

Viewed 318

I'm working on a function that should check if a given dateString has the same timeZone as the browser timeZone. To get the browser timeZone I can use either a) Intl.DateTimeFormat().resolvedOptions().timeZone which will return Europe/Amsterdam for example or b) new Date().getTimeZoneOffset() that will return -60. Both are fine.

The tricky part is to get the timeZone from the dateString I want to pass, for example from: 2021-01-01T00:00:00-05:00 (which should be America/New_York or 300 AFAIK). How can I get the timeZone from that date? I tried to do: new Date('2021-01-01T00:00:00-05:00').getTimeZoneOffset() but that will convert it to the timeZone of my browser again, returning -60.

Example of function:

function isSameTimeZone(date) {
  // function to get time zone here


  const a = getTimeZone(date)
  return a === new Date().getTimeZoneOffset() || Intl.DateTimeFormat().resolvedOptions().timeZone
}

Testcases

2021-01-01T00:00:00-08:00 (SF)

2021-01-01T00:00:00-05:00 (NY)

2021-01-01T00:00:00+05:30 (Mumbai)

2021-01-01T00:00:00+01:00 (Amsterdam)

Anyone out there with a solution? Thanks in advance!

2 Answers

Here's my method;

function checkTimezone(dateString) {
  const testDate = new Date(dateString);
  const dateRegex = /\d{4}-\d{2}-\d{2}/;
  const myDate = new Date(testDate.toISOString().match(dateRegex)[0]);
  
  return !(testDate - myDate);
}

const testCases = ['2021-01-01T00:00:00-05:00', '2021-01-01T00:00:00-08:00', '2021-01-01T00:00:00-05:00', '2021-01-01T00:00:00+05:30', '2021-01-01T00:00:00+01:00'];
testCases.forEach(testCase => console.log(checkTimezone(testCase)));

So here's how it works, you pass in your date string and create a new Date() instance with it.

Then I get the ISO string with the Date.ISOString() method and match it with the original string to get the date and create another Date instance from it without the time.

Then I find the difference (comes in milliseconds), and convert it to minutes

So I've been testing around a bit and in the end I came up with the following solution, based on the dates I provided in my question.

  const getTimeZoneOffsetFromDate = date => {
    /*
      Check if the offset is positive or negative
      e.g. +01:00 (Amsterdam) or -05:00 (New York)
    */
    if (date.includes('+')) {
      // Get the timezone hours
      const timezone = date.split('+')[1]
      // Get the hours
      const hours = timezone.split(':')[0]
      // Get the minutes (e.g. Mumbai has +05:30)
      const minutes = timezone.split(':')[1]

      /*
        Amsterdam:
        const offset = 01 * -60 = -60 + -0 = -60
      */
      const offset = hours * -60 + parseInt(-minutes)

      return offset === new Date().getTimezoneOffset()
    }

    // Repeat
    const timezone = date.slice(date.length - 5)
    const hours = timezone.split(':')[0]
    const minutes = timezone.split(':')[1]

    /*
      New York:
      const offset = 05 * 60 = 300 + 0 = 300
    */
    const offset = hours * 60 + parseInt(minutes)

    return offset === new Date().getTimezoneOffset()
  }

  console.log(getTimeZoneOffsetFromDate('2021-01-01T00:00:00+01:00'))
Related