“JavaScript Compare fechas” Código de respuesta

Compare las 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
}
Grepper

Compare las 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
}
Jealous Jackal

String Compare en fecha en JS

var d1 = Date.parse("2012-11-01");
var d2 = Date.parse("2012-11-04");
if (d1 < d2) {
    alert ("Error!");
}
Winged Ketchup

Cómo realizar la comparación de la fecha con el objeto de fecha en JavaScript

const compareDates = (d1, d2) => {
  let date1 = new Date(d1).getTime();
  let date2 = new Date(d2).getTime();

  if (date1 < date2) {
    console.log(`${d1} is less than ${d2}`);
  } else if (date1 > date2) {
    console.log(`${d1} is greater than ${d2}`);
  } else {
    console.log(`Both dates are equal`);
  }
};

compareDates("06/21/2022", "07/28/2021");
compareDates("01/01/2001", "01/01/2001");
compareDates("11/01/2021", "02/01/2022");

// This will return:

// "06/21/2022 is greater than 07/28/2021"
// "Both dates are equal"
// "11/01/2021 is less than 02/01/2022"
OHIOLee

Compare la fecha y la hora en JS

var isLarger = new Date("2-11-2012 13:40:00") > new Date("01-11-2012 10:40:00");
Clumsy Cicada

JavaScript Compare fechas

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");
}
Scary Sardine

Respuestas similares a “JavaScript Compare fechas”

Preguntas similares a “JavaScript Compare fechas”

Más respuestas relacionadas con “JavaScript Compare fechas” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código