How to know date is today?

They will never match because you're comparing two separate Date object instances.

You need to get some common value that can be compared. For example .toDateString().

today.toDateString() == today2.toDateString();  // true

If you just compare two separate Date objects, even if they have the exact same date value, they are still different.

For example:

today == new Date( today );  // false

They are the same date/time value, but are not the same object, so the result is false.


function today(td) {
    var d = new Date();
    return td.getDate() == d.getDate() && td.getMonth() == d.getMonth() && td.getFullYear() == d.getFullYear();
}

Tags:

Javascript