简体   繁体   English

使用Javascript,如何确保日期范围有效?

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

In JavaScript, what is the best way to determine if a date provided falls within a valid range? 在JavaScript中,确定提供的日期是否在有效范围内的最佳方法是什么?

An example of this might be checking to see if the user input requestedDate is part of the next valid work week. 这方面的一个示例可能是检查用户输入requestedDate是否是下一个有效工作周的一部分。 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. 请注意,这不仅仅是检查一个日期是否大于另一个日期,因为有效日期等于或大于范围的下限,同时小于或等于范围的上限。

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. 现在关于这段代码需要注意Date.parseDate.parse函数应该适用于大多数输入类型,但是已经知道某些格式的问题,例如“YYYY MM DD”,所以你应该在使用之前测试它。 However, I seem to recall that most browsers will interpret the date string given to Date.parse based upon the computers region settings. 但是,我似乎记得大多数浏览器会根据计算机区域设置解释Date.parse的日期字符串。

Also, the multiplier for 86400000 should be whatever the range of days you are looking for is. 此外,86400000的乘数应该是您要查找的天数。 So if you are looking for dates that are at least one week apart then it should be seven. 因此,如果您正在寻找相隔至少一周的日期,那么它应该是七天。

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: 然后,您需要使用Date.parse解析从UI获取的字符串,如下所示:

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

Does that help? 这有帮助吗?

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. 您可以将各种格式传递给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. 有关详细信息,请参阅http://www.javascriptkit.com/jsref/date.shtmlGoogle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM