gpt4 book ai didi

jQuery Datepicker minDate 计算一天中的时间

转载 作者:行者123 更新时间:2023-12-01 04:51:49 25 4
gpt4 key购买 nike

我在我的网站上使用 Jquery datepicker,以便客户选择所需的交货日期。目前我已将 minDate 设置为 4(周末或节假日除外)。我刚刚意识到我需要考虑一天中的时间,这样如果有人在下午 2 点之后下订单,那么这一天就不会算在 minDate 中(因为发货已经太晚了)。

我找到了一些关于它的帖子,其中一篇似乎特别适用:

How do I restrict dates on datepicker after a certain time

但是,我很难将其应用到我当前的脚本(如下)。如果有人能告诉我如何将其融入其中,那就太酷了。

非常感谢您的时间和帮助! 〜苏珊

<script type="text/javascript">
$(document).ready(function() {

//holidays
var natDays = [
[1, 1, 'uk'],
[12, 25, 'uk'],
[12, 26, 'uk']
];

var dateMin = new Date();
var weekDays = AddBusinessDays(4);

dateMin.setDate(dateMin.getDate() + weekDays);

function AddBusinessDays(weekDaysToAdd) {
var curdate = new Date();
var realDaysToAdd = 0;
while (weekDaysToAdd > 0){
curdate.setDate(curdate.getDate()+1);
realDaysToAdd++;
//check if current day is business day
if (noWeekendsOrHolidays(curdate)[0]) {
weekDaysToAdd--;
}
}
return realDaysToAdd;

}

function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
$('#datepicker').datepicker({
inline: true,
beforeShowDay: noWeekendsOrHolidays,
showOn: "both",
firstDay: 0,
dateformat: "dd/mm/yy",
changeFirstDay: false,
showButtonPanel: true,
minDate: dateMin
});
});
</script>

<p>
<label for="datepicker">Desired Delivery Date: </label>
<input class="input-medium" type="text" id="datepicker" placeholder="ex. 01/01/2013" name="properties[Delivery Date]" readonly />
<label><font size=1>Need it faster? Please call us! (800) 880-0307</font>
</label></p>
<style>
#datepicker { height: 20px; }
#datepicker {-webkit-border-radius: 0 3px 3px 0; -moz-border-radius: 0 3px 3px 0; border-radius: 0 3px 3px 0;}
</style>

最佳答案

我终于解决了这个问题,所以我发布了我自己问题的答案。希望它能帮助某人。下面是完整的代码,minDate 为 4 天,不包括美国国家法定节假日和周末,以及 minDate 调整以排除当天下午 2 点之后的情况。

    <script type="text/javascript">
$(document).ready(function() {

//holidays
var natDays = [
[1, 1, 'New Year'], //2014
[1, 20, 'Martin Luther King'], //2014
[2, 17, 'Washingtons Birthday'], //2014
[5, 26, 'Memorial Day'], //2014
[7, 4, 'Independence Day'], //2014
[9, 1, 'Labour Day'], //2014
[10, 14, 'Columbus Day'], //2013
[11, 11, 'Veterans Day'], //2013
[11, 28, 'Thanks Giving Day'], //2013
[12, 25, 'Christmas'] //2013
];

var dateMin = new Date();
dateMin.setDate(dateMin.getDate() + (dateMin.getHours() >= 14 ? 1 : 0));
AddBusinessDays(dateMin, 4);

function AddBusinessDays(curdate, weekDaysToAdd) {
while (weekDaysToAdd > 0) {
curdate.setDate(curdate.getDate() + 1);
//check if current day is business day
if (noWeekendsOrHolidays(curdate)[0]) {
weekDaysToAdd--;
}
}
}

function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
$('#datepicker').datepicker({
inline: true,
beforeShowDay: noWeekendsOrHolidays,
showOn: "both",
firstDay: 0,
dateformat: "dd/mm/yy",
changeFirstDay: false,
showButtonPanel: true,
minDate: dateMin
});
});
</script>

<p>
<label for="datepicker">Desired Delivery Date: </label>
<input class="input-medium" type="text" id="datepicker" placeholder="ex. 01/01/2013" name="properties[Delivery Date]" readonly />
<label><font size=1>Need it faster? Please call us! (800) 880-0307</font>
</label></p>
<style>
#datepicker { height: 20px; }
#datepicker {-webkit-border-radius: 0 3px 3px 0; -moz-border-radius: 0 3px 3px 0; border-radius: 0 3px 3px 0;}
</style>

关于jQuery Datepicker minDate 计算一天中的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18993639/

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