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

294
Visualizações
To get time zone offset value based on offset name in luxon

Object:

const DateTime = luxon.DateTime;
userInfo = {
   startDate: '14/03/2022',
   startTime: '12:30',
   offsetShort: 'EDT',
 }

From the luxon docs I understood to get offset value, Have to use this method:

DateTime.fromObject(new Date(userInfo.startDate), {zone: 'America/New_York'}).toFormat('ZZ');

Will return the offset value like this: -05:00

Since, My api is not returning zone name(ex: America/New_York), I want to get offset value based the offset short name(i,e IST, EST,EDT). Any possibilities in luxon ??

Basically I need any of the below result based on offset short name(EDT/EST/IST):

DateTime.fromObject(new Date('14-03-2022'), {zone: 'EDT'}).toFormat('ZZ'); >> -05:00

 **OR**

DateTime.fromObject(new Date('14-03-2022'), {zone: 'EDT'}).toFormat('ZZ'); >> America/New_York
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To further illustrate the difficulty here, I've created a findPossibleIanaZones() function. This will take a time zone abbreviation, e.g. EST, EDT, IST and return matching IANA timezone names along with the current offset in minutes.

As you can see, you'll get several matching results, although they will often (but not always!) have the same offset in minutes.

I've used both moment-timezone and luxon to do this, I can't find a luxon-only solution.

I've also created a getOffsetForAbbreviation() function, but it cannot always give us an unambiguous result.

As you can see, IST can refer to Irish Standard Time, Israel Standard Time and of course India Standard Time.

function findPossibleIanaZones(zoneAbbreviation) {
   const timeZones = Object.values(moment.tz._zones).map(z => moment.tz.unpack(z));
   const matchingZones = timeZones.filter(zone => zone.abbrs.includes(zoneAbbreviation));
   return matchingZones.map(z => ({ name: z.name, currentUTCOffsetMinutes: getCurrentOffset(z.name )} )) 
}

function getCurrentOffset(ianaZone) {
    return luxon.IANAZone.create(ianaZone).offset(Date.now());
}

function getUTCOffset(ianaZone, unixTimeMs) {
    return luxon.IANAZone.create(ianaZone).offset(unixTimeMs);
}

function getOffsetForAbbreviation(unixTimeMs, zoneAbbreviation) {
    let zones = findPossibleIanaZones(zoneAbbreviation);
    let offsetNames = zones.map(zone => zone.name);
    let offsets = offsetNames.map(name => getUTCOffset(name, unixTimeMs));
    let uniqueOffsets = [...new Set(offsets)];
    // We should probably throw an error here, null will indicate no value found...
    if (uniqueOffsets.length !== 1) return null;
    return uniqueOffsets[0];
}

const abbrs = ['EDT', 'EST', 'IST'];
for(let abbr of abbrs) { 
    console.log('Timezone abbreviation:', abbr, 'Result:', findPossibleIanaZones(abbr));
    console.log('Current UTC offset:', getOffsetForAbbreviation(Date.now(), abbr));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.0/luxon.min.js" integrity="sha512-2j5fkjQ4q5ceXgfxi+kqrU2Oz234MrpyywZsQz1F5OGnfat7mOhjRr0oz5cpQ+YwwWB+hhDBSyxNGuL/tKWMFw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

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