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

175
Visualizações
How can I get the timestamp in javascript from a string including the timezone db name

I got the following string: "2022/05/01 03:10:00" and I need to create a Date object forcing it to use Chile's UTC offset.

The problem is that because of Daylight saving time (DST) the offset changes twice a year.

How can get that Date object, for example, using the "America/Santiago" TZ db name?

Something like:

new Date("2022/05/01 03:10:00" + getUtcOffset("America/Santiago")).

function getUtcOffset(tzDbName) {
..
}

Returns -3 or -4, depending the time in the year.

EDIT:

I ended using a nice trick for determining if DST was on or off. reference

const dst = hasDST(new Date(strDate));
function hasDST(date = new Date()) {
const january = new Date(date.getFullYear(), 0, 1).getTimezoneOffset();
const july = new Date(date.getFullYear(), 6, 1).getTimezoneOffset();

return Math.max(january, july) !== date.getTimezoneOffset();

}

Then I could create the date with the correct timezone depending on that variable.

if (dst) {
    let d = new Date(strDate + " GMT-0300");
    return d;
} else {
    let d = new Date(strDate + " GMT-0400");
    return d;
}

Thanks everyone!

EDIT2: I finally found a very nice library that does exactly what I was looking for:

https://date-fns.org/v2.28.0/docs/Time-Zones#date-fns-tz

const { zonedTimeToUtc, utcToZonedTime, format } = require('date-fns-tz')
const utcDate = zonedTimeToUtc('2022-05-05 18:05', 'America/Santiago')
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This has been discussed before here. Haven't tested it, but it appears that the simplest solution is:

// Example for Indian time
let indianTime = new Date().toLocaleTimeString("en-US", 
   {timeZone:'Asia/Kolkata',timestyle:'full',hourCycle:'h24'})
console.log(indianTime)

You can check the link for more complex answers and libraries

Generals notes To get the time zone name use:

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

To get the difference from UTC (in minutes) use:

var offset = new Date().getTimezoneOffset();
console.log(offset);
// if offset equals -60 then the time zone offset is UTC+01
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