Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  151] [ 4]  / answers: 1 / hits: 23179  / 12 Years ago, tue, october 9, 2012, 12:00:00

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;

More From » date

 Answers
15

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


[#82661] Monday, October 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryley

Total Points: 118
Total Questions: 81
Total Answers: 102

Location: Kazakhstan
Member since Thu, Dec 23, 2021
3 Years ago
ryley questions
Thu, Sep 2, 21, 00:00, 3 Years ago
Wed, Feb 12, 20, 00:00, 4 Years ago
Wed, Oct 30, 19, 00:00, 5 Years ago
;