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

219
Visualizações
How to convert a date from one type to another typescript

React with Typescript:

I have a date picker(mui v5) that requires date value to be in format "yyyy-MM-dd". However the api return a type Date on the format "2022-01-12T00:00:00.000+00:00"

Attention: There are many answers to create a string in that format but I need a Date without this it won't work with typescrit

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

0

There are basically to ways to do this.

String manipulation

Just use the ISO 8601 string you already have and manipulate it using split() or substring() so that you only have the relevant portion of the string. Use this when you don't need to do any calculation/ further processing with the date.

toLocaleDateString()

Parse the string first by passing it to the Date() constructor then format it using toLocaleDateString() and the locale en-CA to yield the result you need. The advantage of this approach is that you could do some processing that involves the date before eventually converting it to string.

const strManipulationSplit = "2022-01-12T00:00:00.000+00:00".split("T")[0];
const strManipulationSubstring = "2022-01-12T00:00:00.000+00:00".substring(0, 10);
const usingDate = new Date("2022-01-12T00:00:00.000+00:00").toLocaleDateString('en-CA');

console.log(strManipulationSplit);
console.log(strManipulationSubstring);
console.log(usingDate);

about 4 years ago · Juan Pablo Isaza Relatório

0

The conclusion is that it can't be solved through React or JS

If I do:

const strManipulationSplit = "2022-01-12T00:00:00.000+00:00".split("T")[0]; const strManipulationSubstring = "2022-01-12T00:00:00.000+00:00".substring(0, 10); const usingDate = new Date("2022-01-12T00:00:00.000+00:00");

the return will be like: "Tue Dec 28 2021 21:00:00 GMT-0300" not "yyyy-MM-dd"

So I changed the API to return the format "yyyy-MM-dd"

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