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

241
Visualizações
Convert string to date in JavaScript to allow comparison

I have two variables, a date and a time

let date = "30.09.2021";
let time = "13:2224.990";

how I can convert that to a timestamp so I can compare between the dates?

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

0

You can create the timestamp by using the getTime() method, but to do so, you firstly need to create a JavaScript date object which will allow to use the method.

The problem with your inputs is that their format is not compatible with the new Date() method to create the object in the first place, so I use the slice() method to change the format of your date and time variables to do all of the above.

// Inputs
let date = "30.09.2021", time = "13:2224.990";

// Changing format
var year = date.slice(6,10),month = date.slice(3,5),day = date.slice(0,2),slice1 = time.slice(0,5),slice2 = time.slice(5,13), newTime = slice1 + ":" + slice2;

// Creating a Date object
const newDate = new Date(year + "-" + month + "-" + day + "T" + newTime); 

// Seconds since 1970/01/01
var seconds = newDate.getTime() / 1000; 

console.log("Timestamp: " + seconds);

about 4 years ago · Juan Pablo Isaza Relatório

0

The Date.prototype.toISOString method converts date objects to an easy-to-compare format:

const date1 = new Date();
const date2 = new Date();

date1.toISOString() < date2.toISOString();
// `true` if `date1` is earlier than `date2`
// `false` if `date1` is later than or equal to `date2`
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