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

174
Visualizações
Format date and time from UTC with offset using date-fns

I have a UTC time with offset like below. I'm trying to format the UTC date time string using format function from date-fns library.

import { format } from "date-fns";

const utcDateTime = "2021-10-14T21:03:56.3256046+00:00";
const formattedDate = format(new Date(utcDateTime), "MM/dd/yyyy hh:mm");

What I'm expecting is 10/14/2021 21:03, a 24 hour time format but what I get is 10/14/2021 04:03, a converted date time for my timezone.

How to display the date and time exactly like with UTC time instead of converting the date time to local timezone?

I created a working example using CodeSandbox. Could anyone please help?

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

0

After spending a lot of time, I was able to achieve the desired result using the plain JavaScript Date object and its functions.

  • First, parsing the date time string and converting it to ISO string using toISOString() function.

  • Second, splitting the formatted date and time extracts from the ISO string.

Below is the code

const formatToUTCDateTime = (dateString) => {
  const date = new Date(Date.parse(dateString));
  const formattedDate = date.toISOString().split("T")[0].split("-");
  const formattedTime = date.toISOString().split("T")[1].split(":");
  return `${formattedDate[1]}/${formattedDate[2]}/${formattedDate[0]} ${formattedTime[0]}:${formattedTime[1]}`;
};

console.log("Result - ", formatToUTCDateTime("2021-10-14T20:03:56.3256046+00:00"));

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