Javascript-将工作日添加到用户选择的日期 [英] Javascript - Add business days to a user selected date
本文介绍了Javascript-将工作日添加到用户选择的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将用户定义的工作日添加到用户定义的日期,并使其显示在警报框中。
I need to add user defined business days to a user defined date and for it to display in an alert box.
请问一下我的代码并让我知道需要更改什么:
Could you please look at my code and let me know what needs changing:
请参见 JSFiddle
Select Business Days To Add<br>
<select name="coll" id="t1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><p>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script> $(function() { $( "#datepicker" ).datepicker(); }); </script>
<input id="datepicker" onchange="alert()"/>
推荐答案
开始时,编写一个需要选择日期的函数和天:
For a start write a function that takes the selected date and days:
<center>Select Business Days To Add<br>
<select name="coll" id="t1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><p>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script> $(function() { $( "#datepicker" ).datepicker(); });
function getSelectedDate() {
var day = $("#datepicker").datepicker("getDate");
var daysToAdd = $("#t1").val();
return Number(daysToAdd) + Number(day.getDate());
}
</script>
<input id="datepicker" onchange="alert(getSelectedDate());"/>
我假设工作日是指星期一至星期五,所以给星期五加1表示实际上是星期一而不是星期一星期六。
I suppose business days mean from monday to friday, so adding 1 to friday would mean actually monday and not saturday.
进一步
- 您需要查看当天是否为星期五并添加
所选工作日的两天可以跳过周末-使用 getDate() - 如果是月末(关于30和31个月中的均匀和不平坦的月份
天,leap年2月而不是leap年),如果需要,增加$ b $也是b个月-使用 getMonth - if现在是年底,增加年份-使用 getFullYear
- you need to look if the current day is friday and add two days to the selected business days to jump over the weekend - use getDate()
- if it is end of the month (regarding even and uneven months 30 and 31 days and february on leap and not leap year) and if needed increment the month too - use getMonth
- if it is end of the year, increment the year - use getFullYear
代码(无需周末检查)如下所示:
<center>Select Business Days To Add<br>
<select name="coll" id="t1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><p>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script> $(function() { $( "#datepicker" ).datepicker(); });
function isEndOfMonth(date) {
// day of month from 1 to 31
var selectedDay = date.getDate();
// months with 31 days
if (date.getMonth() == 0 || date.getMonth() == 2 ||
date.getMonth() == 4 || date.getMonth() == 6 ||
date.getMonth() == 7 || date.getMonth() == 9 ||
date.getMonth() == 11) {
return selectedDay == 31;
}
// february 28 / 29 days TODO check for leap year!
if (date.getMonth() == 1) { return selectedDay == 28; }
// months with 30 days
if (date.getMonth() == 3 || date.getMonth() == 5 ||
date.getMonth() == 8 || date.getMonth() == 10) {
return selectedDay == 30;
}
return false;
}
function isEndOfYear(month) {
return month > 11;
}
function getSelectedDate() {
var day = $("#datepicker").datepicker("getDate");
var daysToAdd = Number($("#t1").val());
// values from which the new date is constructed
var selectedMonth = day.getMonth();
var selectedYear = day.getFullYear();
var selectedDay = day.getDate();
//
if (isEndOfMonth(day)) {
// start new month
selectedDay = 1;
selectedMonth++;
if (isEndOfYear(selectedMonth)) {
// start new year
selectedYear++;
}
}
// add business days
selectedDay += daysToAdd;
// TODO check if the result is weekend and jump over it. After a jump the same checks as above should be called!
return new Date(selectedYear, selectedMonth, selectedDay, 0, 0, 0, 0);
}
</script>
<input id="datepicker" onchange="alert(getSelectedDate());"/>
例如,如果所选日期为 2013年10月14日, 2 天,函数的结果为 2013年10月16日。
If for example the selected date is 14.Oct.2013, selected days 2, the function yields 16.Oct.2013.
这篇关于Javascript-将工作日添加到用户选择的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文