JS检查多个日期是否在范围内 [英] JS Check if multiple dates are within range

查看:53
本文介绍了JS检查多个日期是否在范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,其中有到达日期和离开日期,并编辑了它们的格式以及禁用日期:

I have the following code where I have an arrival date and departure date and edit their format, as well as a disabled date:

var aankomstDatum = "19-05-2018";
var parts = aankomstDatum.split('-');
aankomstDatumDate = new Date(parts[2],parts[1]-1,parts[0]);

vertrekDatum = "02-06-2018";
var parts2 = vertrekDatum.split('-');
vertrekDatumDate = new Date(parts2[2],parts2[1]-1,parts2[0]);

var aankomstDatumDateCheck = (aankomstDatumDate.getMonth() + 1) + '/' + aankomstDatumDate.getDate() + '/' +  aankomstDatumDate.getFullYear();
//alert(aankomstDatumDateCheck);

var vertrekDatumDateCheck = (vertrekDatumDate.getMonth() + 1) + '/' + vertrekDatumDate.getDate() + '/' +  vertrekDatumDate.getFullYear();
//alert(vertrekDatumDateCheck);

var disabledDates = "26-05-2018";
var partsdisabled = disabledDates.split('-');
var disableddatesDatumDate = new Date(partsdisabled[2], partsdisabled[1]-1, partsdisabled[0]); //alert(disableddatesDatumDate);

var disableddatesDatumDateCheck = (disableddatesDatumDate.getMonth() + 1) + '/' + disableddatesDatumDate.getDate() + '/' +  disableddatesDatumDate.getFullYear();
//alert(disableddatesDatumDateCheck);

if(dateCheck(aankomstDatumDateCheck,vertrekDatumDateCheck,disableddatesDatumDateCheck)) {
    console.log("Not available");
} else {
    console.log("Available");
}

function dateCheck() {
    return true;
}

基本上,如果禁用日期在到达日期和离开日期之间,则if-else触发,并且

Basically, if the disabled date is between the arrival date and departure date, the if-else fires, and in the other case the else.

此代码有效(万岁!),但我还没有。因为我计划将多个日期设为var disabledDates,所以这就是我要坚持的地方。那么,如何编辑检查多个禁用日期的代码?

This code works (hooray!), but I'm not there yet. Because I planned to have multiple dates as var disabledDates and that's where I'm stuck. So, how can edit the code that multiple disabled dates are checked?

推荐答案

这是您代码的简化版本,可按您的要求工作。我认为最好在测试时使用ISO8601 格式的文本构造Date对象,即 2018-05-19 (在UTC中创建日期)。另请参阅答案末尾的提示。

Here's a simplified version of your code, which works as you ask. I think it's better to construct Date objects using ISO8601 formatted text while testing i.e. "2018-05-19" (which creates dates in UTC). Also see tips at the end of the answer.

单击代码下方的运行代码段按钮以查看控制台输出(比使用警报更好):

Click the Run code snippet button below the code to see the console output (much better than using alert):

var start = new Date("2018-05-19");
var end = new Date("2018-06-02");

var bookings = [
    new Date("2018-05-26"),
    new Date("2018-05-28")
];

if (validPeriod(start, end, bookings)) {
    console.log("no bookings found");
} else {
    console.log("found at least one booking");
}

function validPeriod(start, end, bookings) {
    var valid = true;

    for (var i = 0; i < bookings.length; i++) {
        var date = bookings[i];
        if (start <= date && date <= end) {
            valid = false;
            break;
        }
    }

    return valid;
}

提示 >

我强烈建议您使用 Moment.js 处理日期。

I strongly recommend you use Moment.js to work with dates. It'll save you headaches in the future.

如果您不选择Moment.js,请记住,根据创建日期的方式,取决于日期使用时区,并根据要显示日期的计算机时区,一种简单的方法是使用新的Date(年,月,日,小时,分钟,秒)构造函数:

If you don't opt for Moment.js, just remember that depending on how you create the date will depend on which timezone is used, and depending on the timezone of your computer which date will display, One easy way is to use the new Date(year, month, day, hours, minutes, seconds) constructor:

// for a browser/computer in timezone GMT+0200 (Paris)

// create a local date
new Date(2018, 5-1, 18).toString() // "Fri May 18 2018 00:00:00 GMT+0200 (CEST)"
new Date(2018, 5-1, 18).toISOString() // "2018-05-17T22:00:00.000Z"

// create a UTC date
new Date(Date.UTC(2018, 5-1, 18)).toString() // "Fri May 18 2018 02:00:00 GMT+0200 (CEST)"
new Date(Date.UTC(2018, 5-1, 18)).toISOString() // "2018-05-18T00:00:00.000Z"

// for a browser/computer in timezone GMT-0400 (New York)

// create a local date
new Date(2018, 5-1, 18).toString() // "Fri May 18 2018 00:00:00 GMT-0400 (EDT)"
new Date(2018, 5-1, 18).toISOString() // "2018-05-18T04:00:00.000Z"

// create a UTC date
new Date(Date.UTC(2018, 5-1, 18)).toString() // "Thu May 17 2018 20:00:00 GMT-0400 (EDT)"
new Date(Date.UTC(2018, 5-1, 18)).toISOString() // "2018-05-18T00:00:00.000Z"

但是请注意是否为Date构造函数使用字符串,因为它使用 Date.parse(dateString) 在内部,有时是解释为本地日期,有时还表示为UTC:

But watch out if you use a string for the Date constructor because it uses Date.parse(dateString) internally and sometimes it's interpreted as a local date, and sometimes UTC:

// for a browser/computer in timezone GMT-0400 (New York)
new Date("08-19-2018"); // Sun Aug 19 2018 00:00:00 GMT-0400 (EDT) <-- local
new Date("08/19/2018"); // Sun Aug 19 2018 00:00:00 GMT-0400 (EDT) <-- local
new Date("2018-05-19"); // Fri May 18 2018 20:00:00 GMT-0400 (EDT) <-- UTC
new Date("2018/05/19"); // Sat May 19 2018 00:00:00 GMT-0400 (EDT) <-- local

这篇关于JS检查多个日期是否在范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆