Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda