<p>In JavaScript, what is the best way to determine if a date provided falls within a valid range?</p>

<p>An example of this might be checking to see if the user input <code>requestedDate</code> is part of the next valid work week. Note that this is not just checking to see if one date is larger than another as a valid date would be equal to or greater than the lower end of the range while less than or equal to the upper end of the range.</p>

[{'answer_id': 74024, 'author': 'Jesper Blad Jensen', 'author_id': 11559, 'author_profile': 'https://Stackoverflow.com/users/11559', 'pm_score': 3, 'selected': False, 'text': "<p>So if i understand currenctly, you need to look if one date is bigger than the other.</p>\n\n<pre><code>function ValidRange(date1,date2)\n{\n   return date2.getTime() &gt; date1.getTime();\n}\n</code></pre>\n\n<p>You then need to parse the strings you are getting from the UI, with Date.parse, like this:</p>\n\n<pre><code>ValidRange(Date.parse('10-10-2008'),Date.parse('11-11-2008'));\n</code></pre>\n\n<p>Does that help?</p>\n"}, {'answer_id': 74040, 'author': 'rjzii', 'author_id': 1185, 'author_profile': 'https://Stackoverflow.com/users/1185', 'pm_score': 5, 'selected': True, 'text': '<p>This is actually a problem that I have seen come up before a lot in my works and the following bit of code is my answer to the problem.</p>\n\n<pre><code>// checkDateRange - Checks to ensure that the values entered are dates and \n//     are of a valid range. By this, the dates must be no more than the \n//     built-in number of days appart.\nfunction checkDateRange(start, end) {\n   // Parse the entries\n   var startDate = Date.parse(start);\n   var endDate = Date.parse(end);\n   // Make sure they are valid\n    if (isNaN(startDate)) {\n      alert("The start date provided is not valid, please enter a valid date.");\n      return false;\n   }\n   if (isNaN(endDate)) {\n       alert("The end date provided is not valid, please enter a valid date.");\n       return false;\n   }\n   // Check the date range, 86400000 is the number of milliseconds in one day\n   var difference = (endDate - startDate) / (86400000 * 7);\n   if (difference &lt; 0) {\n       alert("The start date must come before the end date.");\n       return false;\n   }\n   if (difference &lt;= 1) {\n       alert("The range must be at least seven days apart.");\n       return false;\n    }\n   return true;\n}\n</code></pre>\n\n<p>Now a couple things to note about this code, the <code>Date.parse</code> function should work for most input types, but has been known to have issues with some formats such as "YYYY MM DD" so you should test that before using it. However, I seem to recall that most browsers will interpret the date string given to Date.parse based upon the computers region settings.</p>\n\n<p>Also, the multiplier for 86400000 should be whatever the range of days you are looking for is. So if you are looking for dates that are at least one week apart then it should be seven.</p>\n'}, {'answer_id': 74051, 'author': 'Grant Wagner', 'author_id': 9254, 'author_profile': 'https://Stackoverflow.com/users/9254', 'pm_score': 3, 'selected': False, 'text': '<pre><code>var myDate = new Date(2008, 9, 16);\n\n// is myDate between Sept 1 and Sept 30?\n\nvar startDate = new Date(2008, 9, 1);\nvar endDate = new Date(2008, 9, 30);\n\nif (startDate &lt; myDate &amp;&amp; myDate &lt; endDate) {\n    alert(\'yes\');\n    // myDate is between startDate and endDate\n}\n</code></pre>\n\n<p>There are a variety of formats you can pass to the Date() constructor to construct a date. You can also construct a new date with the current time:</p>\n\n<pre><code>var now = new Date();\n</code></pre>\n\n<p>and set various properties on it:</p>\n\n<pre><code>now.setYear(...);\nnow.setMonth(...);\n// etc\n</code></pre>\n\n<p>See <a href="http://www.javascriptkit.com/jsref/date.shtml" rel="noreferrer">http://www.javascriptkit.com/jsref/date.shtml</a> or <a href="http://www.google.ca/search?q=javascript+date+object" rel="noreferrer">Google</a> for more details.</p>\n'}]
['https://Stackoverflow.com/questions/73971', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1185/']