Ejemplo 1: comparar fechas en js
var date1 = new Date('December 25, 2017 01:30:00');
var date2 = new Date('June 18, 2016 02:30:00');
//best to use .getTime() to compare dates
if(date1.getTime() === date2.getTime()){
//same date
}
if(date1.getTime() > date2.getTime()){
//date 1 is newer
}
Ejemplo 2: comparar fechas en javascript
var date1 = new Date('December 25, 2017 01:30:00');
var date2 = new Date('June 18, 2016 02:30:00');
//best to use .getTime() to compare dates
if(date1.getTime() === date2.getTime()){
//same date
}
if(date1.getTime() > date2.getTime()){
//date 1 is newer
}
Ejemplo 3: comparación de cadenas en la fecha en js
var d1 = Date.parse("2012-11-01");
var d2 = Date.parse("2012-11-04");
if (d1 < d2) {
alert ("Error!");
}
Ejemplo 4: cómo comparar fechas js
const x = new Date('2013-05-22');
const y = new Date('2013-05-23');
// less than, greater than is fine:
console.log('x < y', x < y); // false
console.log('x > y', x > y); // false
console.log('x === y', x === y); // false, oops!
// anything involving '=' should use the '+' prefix
// it will then compare the dates' millisecond values
console.log('+x <= +y', +x <= +y); // true
console.log('+x >= +y', +x >= +y); // true
console.log('+x === +y', +x === +y); // true
Ejemplo 5: fechas de comparación de JavaScript
let myDate = new Date("January 13, 2021 12:00:00");
let yourDate = new Date("January 13, 2021 15:00:00");
if (myDate < yourDate) {
console.log("myDate is less than yourDate"); // will be printed
}
if (myDate > yourDate) {
console.log("myDate is greater than yourDate");
}
Ejemplo 6: fechas de comparación de JavaScript
let d1 = new Date();
let d2 = new Date();
// can use >, <, <=, <=
d1 > d2
d1 >= d2
// == won't work so can use this:
(d1 >= d2) && (d2 >= d1)
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)