15

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

An example of this might be checking to see if the user input requestedDate 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.

rjzii
  • 14,236
  • 12
  • 79
  • 119

3 Answers3

18

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.

// checkDateRange - Checks to ensure that the values entered are dates and 
//     are of a valid range. By this, the dates must be no more than the 
//     built-in number of days appart.
function checkDateRange(start, end) {
   // Parse the entries
   var startDate = Date.parse(start);
   var endDate = Date.parse(end);
   // Make sure they are valid
    if (isNaN(startDate)) {
      alert("The start date provided is not valid, please enter a valid date.");
      return false;
   }
   if (isNaN(endDate)) {
       alert("The end date provided is not valid, please enter a valid date.");
       return false;
   }
   // Check the date range, 86400000 is the number of milliseconds in one day
   var difference = (endDate - startDate) / (86400000 * 7);
   if (difference < 0) {
       alert("The start date must come before the end date.");
       return false;
   }
   if (difference <= 1) {
       alert("The range must be at least seven days apart.");
       return false;
    }
   return true;
}

Now a couple things to note about this code, the Date.parse 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.

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.

rjzii
  • 14,236
  • 12
  • 79
  • 119
  • pls update fromat of start and end checkDateRange(start, end) its very difficult to chk – ND's Jul 04 '12 at 13:17
  • `let difference = (endDate - startDate) / (86400000 * [n° days]); if (difference <= 1) {inside the range} else {out of range }` This helped me to see if the two dates are within a range x – Diego Lope Loyola Nov 02 '21 at 06:18
10

So if i understand currenctly, you need to look if one date is bigger than the other.

function ValidRange(date1,date2)
{
   return date2.getTime() > date1.getTime();
}

You then need to parse the strings you are getting from the UI, with Date.parse, like this:

ValidRange(Date.parse('10-10-2008'),Date.parse('11-11-2008'));

Does that help?

Jesper Blad Jensen
  • 2,751
  • 17
  • 16
  • The problem is not so much checking to see if one date is larger than the other but checking to see if one date is within a given range valid dates. For example, if the business requirement is that a task is addressed in the next seven days then you want to ensure that the scheduled date is less than or equal to seven days from the current date. – rjzii May 25 '10 at 17:42
5
var myDate = new Date(2008, 9, 16);

// is myDate between Sept 1 and Sept 30?

var startDate = new Date(2008, 9, 1);
var endDate = new Date(2008, 9, 30);

if (startDate < myDate && myDate < endDate) {
    alert('yes');
    // myDate is between startDate and endDate
}

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:

var now = new Date();

and set various properties on it:

now.setYear(...);
now.setMonth(...);
// etc

See http://www.javascriptkit.com/jsref/date.shtml or Google for more details.

Grant Wagner
  • 25,263
  • 7
  • 54
  • 64