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

791
Vistas
Changing time according to TimeZone in React

I have a method that gets a parameter as a time string "15:35". That time is in UTC TimeZone. But I wanna convert it to different TimeZone (for example America/New_York or ...).

The examples say that there is a package called "moment" that has a "tz" method but I checked and there is no such method in that package. Also I mustn't update package or install one. How could I convert given time to according to given timezone without parsing?

Example : Given "18:35" in UTC -> Expected 14:35 in America/New_York

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

0

I suggest that you should use moment because it's very easy to use if compared to pure javascript/typescript.

You can use the below method to do it:

moment.tz.setDefault(String)

In your case, you can set the timezone to America/New_York by:

moment.tz.setDefault("America/New_York")

You can find out more at https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/

about 4 years ago · Juan Pablo Isaza Denunciar

0

I don't really understand why you can't install a package. If you are able to use moment.js (which I would recommend) and your string is no date string (like "2021-09-22T15:35:00Z") you could solve it like that:

function changeDatetimeByTimezone(datetime, timezone) {
    const parsedDateAsUtc = moment.utc()
      .startOf('day') 
      .add(datetime.substring(0, 2), "hours")
      .add(datetime.substring(3, 5), "minutes");
    return parsedDateAsUtc.clone().tz(timezone).format("hh:mm");
}

and then use it like this:

const dateTimeNY = changeDatetimeByTimezone("15:35", "America/New_York"); // 11:35
const dateTimeBE = changeDatetimeByTimezone("15:35", "Europe/Berlin"); // 17:35
about 4 years ago · Juan Pablo Isaza Denunciar

0

Install moment-timezone package. It will help to convert time. Refer below link.

https://momentjs.com/timezone/

How to use:

1.Import both below packages in top of your component file.

import moment from "moment";
import 'moment-timezone';

2.Convert the time as follows.

var localTime = moment.utc('18:35', "HH:mm").tz('America/New_York').format("HH:mm");
console.log(localTime);
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