Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda