gpt4 book ai didi

javascript - 设置验证以使用 javascript 检查两个日期

转载 作者:行者123 更新时间:2023-11-28 04:39:25 25 4
gpt4 key购买 nike

我试图找到两个日期之间的差异,当用户想要更改到达日期时,记住更改前到达/离开之间的天数,更改到达日期后,自动将出发日期设置为x天后到达日期,

因此,如果我的日期为 2017 年 6 月 1 日 - 2017 年 6 月 5 日(4 天),并且用户将抵达日期更改为 2017 年 6 月 4 日,然后将出发日期设置为 2017 年 6 月 8 日(+ 4 天)

function changedDate() {

var startDate = $("#Arrival").val().split("-");
var endDate = $("#Departure").val().split("-");
var arrivalDate = new Date(startDate[2], startDate[1] - 1, startDate[0]);
var departureDate = new Date(endDate[2], endDate[1] - 1, endDate[0]);
var timeDiff = Math.abs(departureDate.getDate() - arrivalDate.getDate());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));


if (arrivalDate >= departureDate) {
var arrDate = arrivalDate;
arrDate.setDate(arrDate.getDate() + 1);

var month = arrDate.getMonth() + 1;
if (month < 10)
month = '0' + month;
var day = arrDate.getDate();
if (day < 10)
day = '0' + day;
var year = arrDate.getFullYear();


$("#Departure").val(day + '-' + month + '-' + year);
}

if (timeDiff > 1) {
var arrDate = arrivalDate;
var depDate = departureDate;
arrDate.setDate(arrDate.getDate());
depDate.setDate(depDate.getDate() + diffDays);

var month = arrDate.getMonth() + 1;
if (month < 10)
month = '0' + month;
var day = arrDate.getDate();
if (day < 10)
day = '0' + day;
var year = arrDate.getFullYear();

var monthd = depDate.getMonth() + 1;
if (monthd < 10)
monthd = '0' + month;
var dayd = depDate.getDate();
if (dayd < 10)
dayd = '0' + day;
var yeard = depDate.getFullYear();

$("#Arrival").val(day + '-' + month + '-' + year);
$("#Departure").val(dayd + '-' + monthd + '-' + yeard);
}

if(arrivalDate < departureDate) {
var arrDate = arrivalDate;
arrDate.setDate(arrDate.getDate() + 1);

var month = arrDate.getMonth() + 1;
if (month < 10)
month = '0' + month;
var day = arrDate.getDate();
if (day < 10)
day = '0' + day;
var year = arrDate.getFullYear();

$("#Departure").val(day + '-' + month + '-' + year);

}

}

this condition not related with the validation that i want this is to set departureDate +1 after arrival date on change

if(到达日期 >= 出发日期)

this condition not related with the validation that i want this is to set departureDate +1 after arrival date on change

if(到达日期 < 出发日期)

this condition i make it for this validation but didn't work

如果(时间差 > 1)

最佳答案

不要使用 getDate() 方法来计算 timeDiff,而是使用 getTime() 并执行简单的减法,然后执行毫秒到天的重构。

类似这样的事情:

var startDate = $("#Arrival").val().split("-");
var endDate = $("#Departure").val().split("-");
var arrivalDate = new Date(startDate[2], startDate[1] - 1, startDate[0]);
var departureDate = new Date(endDate[2], endDate[1] - 1, endDate[0]);
var timeDiff = Math.abs(departureDate.getTime() - arrivalDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));

此外,在减去Date类型值时,getTime()方法变为可选,也可以直接减去departureDate -arrivalDate

关于javascript - 设置验证以使用 javascript 检查两个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43913260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com