xxxxxxxxxx
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
}
xxxxxxxxxx
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"
xxxxxxxxxx
function compareDatesWithoutTime(date1, date2) {
const strippedDate1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
const strippedDate2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
return strippedDate1.getTime() === strippedDate2.getTime();
}
// Example usage
const date1 = new Date('2022-01-01T10:00:00');
const date2 = new Date('2022-01-01T15:30:00');
const result = compareDatesWithoutTime(date1, date2);
console.log(result); // Output: true
xxxxxxxxxx
var d1 = Date.parse("2012-11-01");
var d2 = Date.parse("2012-11-04");
if (d1 < d2) {
alert ("Error!");
}
xxxxxxxxxx
// We initialize the Date() object with the current date and time
const date1 = new Date();
// We initialize a past date
const date2 = new Date('2018-04-07 12:30:00');
// Let's see if the first date is equal, more recent or less recent than the second date
if (date1.getTime() === date2.getTime()) {
console.log('The dates are equal');
}
else if (date1.getTime() > date2.getTime()) {
console.log(date1.toString() + ' is more recent than ' + date2.toString());
}
else {
console.log(date1.toString() + ' is less recent than ' + date2.toString());
}
xxxxxxxxxx
let date1 = new Date("2024-01-29T03:34:48.000Z"); // Tue Jan 29 2024 03:34:48 GMT+0000
let date2 = new Date("2024-01-29T15:00:00.000Z"); // Tue Jan 29 2024 15:00:00 GMT+0000
let date1String = date1.toDateString(); // "Tue Jan 29 2024"
let date2String = date2.toDateString(); // "Tue Jan 29 2024"
console.log(date1String === date2String); // Output: true
console.log(date1String < date2String); // Output: false
console.log(date1String > date2String); // Output: false
xxxxxxxxxx
var isLarger = new Date("2-11-2012 13:40:00") > new Date("01-11-2012 10:40:00");
xxxxxxxxxx
var x = new Date('2013-05-23');
var y = new Date('2013-05-23');
// less than, greater than is fine:
x < y; => false
x > y; => false
x === y; => false, oops!
// anything involving '=' should use the '+' prefix
// it will then compare the dates' millisecond values
+x <= +y; => true
+x >= +y; => true
+x === +y; => true
xxxxxxxxxx
// solution is convert date to time by getTime()
start = startDate.getTime();
end = endDate.getTime();
current = date.getTime();
if (start <= current && current <= end) {
// do something here
}
xxxxxxxxxx
const isSameDate = date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate();
// the above implies they are in the same timezone
// if there's a possibility they are in different timezones
// you must convert them first to UTC before comparing