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

101
Vistas
Converting time to another timezone JS

I'm obtaining data.value which is a time in the format: hh:mm a - for example 12:30 am.

I also know:

  1. the local timezone of the user (userTimeZone)
  2. the timezone of the venue (venueTimeZone)

I need to convert the time selected by the user (data.value) to the correct date in the venueTimeZone. For example, if the user is in Americas/New York and they selected 1:30PM on the 20/05/2022, and the venue is in Americas/Los Angeles - the value I am interested in obtaining is 20/05/2022 10:30AM.

This is my attempt, however the timezone itself doesn't change - I think this is because when I create the userDateTime with moment I don't specify a time offset, but I'm not sure how to obtain the offset from userTimeZone, whilst accounting for DST.

            const userTimeZone = _.get(
                Intl.DateTimeFormat().resolvedOptions(),
                ['timeZone']
            );

            const venueDateStr = new Date().toLocaleString('en-US', {
                timeZone: venueTimeZone,
            });

            const Date = new Date(restaurantDateStr);
            const venueYear = venueDate.getFullYear();
            const venueMonth = `0${venueDate.getMonth() + 1}`.slice(-2);
            const venueDateOfMonth = `0${venueDate.getDate()}`.slice(-2);

            const userDateTime = createDateAsUTC(
                moment(
                    `${venueDateOfMonth}/${venueMonth}/${venueYear} ${data.value}`,
                    'DD/MM/YYYY hh:mm a'
                ).toDate()
            ).toLocaleString('en-US', { timeZone: venueTimeZone });

EDIT - I do not have the city offset, I have the timezone name, therefore I cannot use any suggested answer which relies on city offset.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Consider using Luxon - the successor to Moment. (See Moment's project status.)

// Parse your input string using the user's local time zone
// (this assumes the current local date)
const local = luxon.DateTime.fromFormat('1:30 pm', 'h:mm a');

// Convert to the desired time zone
const converted = local.setZone('America/Los_Angeles');

// Format the output as desired
const formatted = converted.toFormat('dd/MM/yyyy h:mm a').toLowerCase();

console.log(formatted);
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.4.0/luxon.min.js"></script>

You could also do this without a library, however you may find that not all browsers will parse the input string, and your output format is up to the browser as well.

// Get the current local date as a string
const date = new Date().toLocaleDateString();

// Parse the date and time in the local time zone
// Warning: This is non-standard and may fail in some environments
const dt = new Date(date + ' 1:30 pm');

// Format the output, converting to the desired time zone
const result = dt.toLocaleString(undefined, { timeZone: 'America/Los_Angeles' });

console.log(result);

There are, of course, manual ways to parse and format dates (using regex, etc.) but I'll leave that up to you or another person to complete.

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