Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two dates not on the same calendar day

How to check if two dates not on the same day. I came up with this solution but maybe there is a better way to do this:

 var actualDate = new Date();  var isNotToday = dateToCheck.getDay() !== actualDate.getDay() || dateToCheck < actualDate - 24 * 60 * 60 * 1000; 
like image 960
Andreas Köberle Avatar asked Oct 09 '12 14:10

Andreas Köberle


People also ask

How can you tell if two date objects are on the same day?

To check if two dates are the same day, call the toDateString() method on both Date() objects and compare the results. If the output from calling the method is the same, the dates are the same day.

How do you check if a date exists between two dates?

To check if a date is between two dates: Use the Date() constructor to convert the dates to Date objects. Check if the date is greater than the start date and less than the end date. If both conditions are met, the date is between the two dates.

How do you know if two dates are equal in typescript?

Call the getTime() method on each date to get a timestamp. Compare the timestamp of the dates. If a date's timestamp is greater than another's, then that date comes after.


1 Answers

Another option is using .toDateString() function to parse both dates into strings. The function formats output to: "Wed Jul 28 1993." Then you can compare both date strings.

actualDate.toDateString() === dateToCheck.toDateString() // returns true if actualDate is same day as dateToCheck 

Here's a Plunker:

http://plnkr.co/edit/J5Dyn78TdDUzX82T0ypA

And more from MDN:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

like image 164
Stu Stein Avatar answered Oct 02 '22 16:10

Stu Stein