I am trying to compare old date with today's date but I am not getting output true. Please help me.
let today = new Date(); // 2021-02-13T13:28:28.501Z
if ("2020-11-04T08:01:25.698Z" < today){
console.log("True");
}
I am trying to compare old date with today's date but I am not getting output true. Please help me.
let today = new Date(); // 2021-02-13T13:28:28.501Z
if ("2020-11-04T08:01:25.698Z" < today){
console.log("True");
}
Variable today is a date object you need to compare it with date object only and then only you will get the desired results
if (new Date() === today ) console.log('true')```
You can compare two dates (Date objects) using getTime method
console.log(date1.getTime() > date2.getTime())
This example prints true if date1 is later than date2
In your case:
let today = new Date();
let otherDay = new Date("2020-11-04T08:01:25.698Z");
if (otherDay.getTime() < today.getTime()){
console.log("True");
}
You need to convert string to date first
if (new Date("2020-11-04T08:01:25.698Z") < today){
console.log("True");
}
let today = new Date(); // 2021-02-13T13:28:28.501Z
if (new Date("2020-11-04T08:01:25.698Z") < today)
{
console.log("True");
}
Convert string to Date object and compare.
you can create an isTodaySameDay function and call it. check out the snippet code below:
const isTodaySameDay = (day) => {
const today = new Date();
return day.getFullYear() === today.getFullYear() &&
day.getMonth() === today.getMonth() &&
day.getDate() === today.getDate();
};
let yesterday = new Date();
yesterday.setDate(yesterday .getDate() - 1);
console.log(isTodaySameDay(yesterday)); //false
let today = new Date();
console.log(isTodaySameDay(today)); //true