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;
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.
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.
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.
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
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