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

199
Vistas
How to get the UTC time given any timezone and hour (considering daylight savings)

An example would be 9am CST (central standard time), or 6am CET (Central European Time).

Using any new Date() calls will always give me a date in my local time zone (BST, or GMT+1).

I think I need to first create the date in the desired timezone, no?

function convertToUTC(date, tzString) {
   // some code?  
}

let resp = convertToUTC("2022-07-12 09:00:00", "America/Chicago");

console.log(resp)

should be 
"2022-07-12T14:00:00Z"
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Based on MDN documentation.

console.log(new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'long' }).format(date));

This will output

// Expected output "Sunday, 20 December 2020 at 14:23:16 GMT+11"

There are many options, and many locales. You can check them all out here.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I would definitely suggest using a specialized date/time library for this purpose.

Timezone conversion in vanilla JavaScript is tricky to say the least (especially converting from a timezone to UTC)

However this is quite straightforward in luxon:

I'm using the DateTime.fromSQL() function here since it matches the input timestamp format.

const { DateTime } = luxon;

function convertToUTC(date, tzString) {
   const dt = DateTime.fromSQL(date, { zone: tzString });
   return dt.setZone('UTC');
}

console.log(convertToUTC("2022-07-12 09:00:00", "America/Chicago"));
console.log(convertToUTC("2022-07-12 09:00:00", "America/Los_Angeles"));
console.log(convertToUTC("2022-07-12 09:00:00", "Europe/London"));
.as-console-wrapper { max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.1/luxon.min.js" integrity="sha512-Nw0Abk+Ywwk5FzYTxtB70/xJRiCI0S2ORbXI3VBlFpKJ44LM6cW2WxIIolyKEOxOuMI90GIfXdlZRJepu7cczA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

We can do this in vanilla JavaScript, it's just a little awkward.

We're essentially searching for the UTC date that will, when converted to the target timezone, equal the target date.

I'm using the 'sv' locale since it will give us ISO timestamps.

function convertToUTC(date, tzString) {
    const dt = Date.parse(toIso(date) + 'Z');
    for(let offsetMinutes = -840; offsetMinutes <= 660; offsetMinutes += 15) {
        const utc = new Date(dt + offsetMinutes * 60000);
        if (toIso(date) === toIso(utc, tzString)) { 
            return toIso(utc, 'UTC') + 'Z';
        }
    }
}

function toIso(date, timeZone) { 
    return new Date(date).toLocaleString('sv', { timeZone }).replace(' ', 'T');
}

console.log(convertToUTC("2022-07-12 09:00:00", "America/Chicago"));
console.log(convertToUTC("2022-07-12 09:00:00", "America/Los_Angeles"));
console.log(convertToUTC("2022-07-12 09:00:00", "Europe/London"));
.as-console-wrapper { max-height: 100% !important; }

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