Using Javascript, how do I make sure a date range is valid?

前端 未结 3 734
再見小時候
再見小時候 2020-12-01 23:46

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 r

相关标签:
3条回答
  • 2020-12-02 00:27

    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?

    0 讨论(0)
  • 2020-12-02 00:31

    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.

    0 讨论(0)
  • 2020-12-02 00:33
    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.

    0 讨论(0)
提交回复
热议问题