I have two javascript dates and I want to know if they're the same date. The two dates can be for the same date but have different times: Date1 can be set for 3PM while Date2 can be set for 1AM.
I tried these two options but neither work:
// doesn't work when same date but different time
if (Date1.getTime() === Date2.getTime())
// gives true when dates are in different months
if (Date1.getUTCDate() === Date2.getUTCDate())
What the best way to get true when Date1 and Date2 are the same day, regardless of the actual time within the day?
Thanks.
You can get the day, month and year of the dates and check if they are equal.
if (date1.getUTCDate() == date2.getUTCDate() &&
date1.getUTCMonth() == date2.getUTCMonth() &&
date1.getUTCFullYear() == date2.getUTCFullYear()) {
// dates are on the same day
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With