Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda