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

303
Visualizações
Convert a string to date in JavaScript

I have the following string and I want to convert it to Date

'20220722T141444Z'

In C# I converted it by telerik's DateParser library.

Is there a way to convert it in JavaScript as well?

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

0

There are many questions on parsing timestamps. In this case, you can just get the parts as two digit pairs then use that to call the Date constructor.

Since the timestamp has a trailing "Z", likely it should be parsed as UTC so:

let ts = '20220722T141444Z'
let [C,Y,M,D,H,m,s] = ts.match(/\d\d/g) || [];
let date = new Date(Date.UTC(C+Y, M-1, D, H, m, s));

console.log(date.toISOString());

There are many other ways to approach the problem, such as modifying the values to create a supported format such as `2022-07-22T14:14:44Z', however it's more efficient to give the parts directly to the Date constructor than to build a string then have the built–in parser parse it.

about 4 years ago · Juan Pablo Isaza Relatório

0

there is a way in javascript but i think it wont work with this format

change the format like this and try

new Date('20220722T141444Z')
Invalid Date
new Date('20220722')
Invalid Date
new Date('2022 07 22')
Fri Jul 22 2022 00:00:00 GMT+0530 (India Standard Time)
about 4 years ago · Juan Pablo Isaza Relatório

0

Because new Date(string) returns an Invalid Date I'd do it like that and use String.prototype.slice():

function date(a) {
    return `${a.slice(6, 8)}.${a.slice(4, 6)}.${a.slice(0, 4)}`;
}

console.log(date("20220722T141444Z"));
// output: 22.07.2022
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