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

333
Visualizações
Javascript: convert a string containing date & time to a string in the UTC timezone

I have a string in the format below:

yyyy-mm-dd hh:mm

This string is based on the timezone on the client browser. I need to convert this to a string showing the time in UTC.

I can calculate how many hours the offset is using:

new Date().getTimezoneOffset() / 60

Which is fine if the time doesn't take it back by a day (eg. if there's a -2 hour offset to apply to a 1am time).

What would be the best way to achieve this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

With this code you could convert the date strings on client side to corresponding UTC time.

let now = new Date()  // Thu Oct 28 2021 15:31:17 GMT+0200

// this Date in your given format would be >>

let dateStr = '2021-10-28 15:31'

let utcDate = new Date(dateStr).toUTCString()

console.log(utcDate) // Thu, 28 Oct 2021 13:31:00 GMT  << -02:00 to my local time above

let isoDate = new Date(dateStr).toISOString()

console.log(isoDate) // 2021-10-28T13:31:00.000Z  << better format for storing dates

about 4 years ago · Juan Pablo Isaza Relatório

0

I suggest using luxon because you can do various things on it.

Import {DateTime} from "luxon";
DateTime.locale().setZone("your zone").fromFormat("2021-10-10 22:00", "yyyy-MM-dd HH:mm").toUTC();

Hope it works cause wrote with my phone.

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