Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing javascript dates

Tags:

javascript

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.

like image 911
frenchie Avatar asked Feb 12 '26 20:02

frenchie


1 Answers

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
}
like image 168
Aamir Avatar answered Feb 15 '26 09:02

Aamir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!