views:

6191

answers:

5

As the title states, using Javascript, how can I valid two given dates to ensure that they are a valid range? For example, if you have the date 1 January 2010 and the date 6 January 2010, what is the best way to check to see if 6 January 2010 is within seven days of 1 January 2010.

A: 

There is a date object in js that can take a string and then parse it and give you methods to interrogate this king of thing.

you can find the docs at w3schools, javascript:date

DevelopingChris
+5  A: 

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 aka. Deldy
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.
Rob
+4  A: 

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.

// checkDateEntries - Checks to ensure that the values entered are dates and 
//       are of a valid range.
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.prase 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.

Rob
+1  A: 
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
A: 

There is a little more to it than what Rob mentioned, at least for my definition of valid date. JavaScript converts dates so 2/31/2008 becomes 3/3/2008. The easy way to validate that is to compare the day, month and year that you get back from the data object with the day, month and year you pass to the constructor.

/Allan

Allan Wind
Good point on the 2/31/2008 issue, but generally when I do Javascript validation it is not the end all and better validation is done server side. That particular issue would be caught once you try and parse the date on the server.
Rob