Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Fetch UTC Date offset based on date format

I have date with format '2021-05-01T23:59:59.999-05:00'

Need to fetch utc offset value like 300, 330 etc.

Can someone help here.Any Answer without using moment.js is appreciated.

So extracting value -05:00 from '2021-05-01T23:59:59.999-05:00'

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The function Date.getTimezoneOffset() will only ever give you the offset between the client machine timezone and UTC, it won't give you the UTC offset as specified in the ISO date string.

Given that the dates are in ISO-8601 format, we can parse the UTC offset from the data, it will be in the format ±[hh]:[mm], ±[hh][mm], ±[hh] or 'Z' for UTC / Zulu time.

Negative UTC offsets describe a time zone west of UTC±00:00, where the civil time is behind (or earlier) than UTC so the zone designator will look like "−03:00","−0300", or "−03".

Positive UTC offsets describe a time zone at or east of UTC±00:00, where the civil time is the same as or ahead (or later) than UTC so the zone designator will look like "+02:00","+0200", or "+02".

We'll use regular expressions to parse the timezone offsets, this will work on IE 11 too.

function getUTCOffsetMinutes(isoDate) {
    // The pattern will be ±[hh]:[mm], ±[hh][mm], or ±[hh], or 'Z'
    const offsetPattern = /([+-]\d{2}|Z):?(\d{2})?\s*$/;
    if (!offsetPattern.test(isoDate)) {
        throw new Error("Cannot parse UTC offset.")
    }
    const result = offsetPattern.exec(isoDate);
    return (+result[1] || 0) * 60 + (+result[2] || 0);
}

const inputs = [
    '2021-05-01T23:59:59.999+12:00',
    '2021-05-01T23:59:59.999+10',
    '2021-05-01T23:59:59.999+0530',
    '2021-05-01T23:59:59.999+0300',
    '2021-05-01T23:59:59.999Z',
    '2021-05-01T23:59:59.999-05:00',
    '2021-05-01T23:59:59.999-07:00',
];

for(var i = 0; i < inputs.length; i++) {
    console.log('input:', inputs[i], 'offsetMinutes:', getUTCOffsetMinutes(inputs[i]));
}

about 4 years ago · Juan Pablo Isaza Report

0

Pretty much the same as @TerryLennox but deals with:

  1. Seconds in the offset—fairly common before 1900, added as decimal part of minutes
  2. Missing offset—local, returns undefined

function parseOffset(ts) {
  let [all,hr,min,sec] = (''+ts).match(/([+-]\d{2}|Z):?(\d{2})?:?(\d{2})?$/) || [];
  let sign = hr < 0 ? -1 : 1;
  return !all ? all :     // No match, return undefined
         hr == 'Z' ? 0 :
         hr * 60 + (min? sign * min : 0) + (sec? sign * sec / 60 : 0);
}

['2021-05-01T23:59:59.999-05:30',
 '2021-05-01T23:59:59.999+05:30',
 '2021-05-01T23:59:59.999-0530',
 '2021-05-01T23:59:59.999+0530',
 '2021-05-01T23:59:59.999-05',
 '2021-05-01T23:59:59.999+05',
 '2021-05-01T23:59:59.999+053012',
 '2021-05-01T23:59:59.999+05:30:12',
 '2021-05-01T23:59:59.999Z',
 '2021-05-01T23:59:59.999',
 'blah'
].forEach(ts => console.log(ts + ' -> ' + parseOffset(ts)));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!