Javascript 计算明年的同一个工作日
Javascript calculate same weekday next year
我在计算明年给定日期的星期几时遇到了一些麻烦。例如:星期二,19。2016 年 4 月是给定的日期。现在我会计算:星期二,2017 年 4 月 18 日。工作日始终相同是非常重要的。
我的问题是闰年。我希望你能帮助我。
$('.changeYear').click(function() {
var appointments = [];
for (var i = 1; i <= 22; i++) {
var date = $('#tr-fix-tag-' + i).val();
if (date !== "") {
var splittedDate = date.substr(5).split('.');
appointments.push(new Date(splittedDate[2], splittedDate[1] - 1, splittedDate[0]));
}
}
var counter = 1;
$.each(appointments, function() {
var field = $('#tr-fix-tag-' + counter);
var date = $(this)[0];
var newDate = new Date(date.setFullYear(date.getFullYear() + 1));
if (isSchaltjahr(newDate.getFullYear()))
field.val(formatDate(new Date(newDate.setDate(newDate.getDate() - 2))));
else
field.val(formatDate(new Date(newDate.setDate(newDate.getDate() - 1))));
counter++;
});
});
我会用这个,然后朝着你的目标日期的方向走:
var DayOfWeek=[
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
function GetDayOfWeek(year, month, day)
{
var monthAdjust = [ 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 ];
year -= (month < 3) ? 1 : 0;
var leap = Math.floor(year / 4);
var greg = Math.floor(year / 100);
var modern = Math.floor(year / 400);
return DayOfWeek[(year + leap - greg + modern + monthAdjust[month - 1] + day) % 7];
}
这是我让它工作的最快方法。这可能会被优化很多。
这是一个DEMOhttps://jsfiddle.net/92fLoryo/
它基本上是获取明年的日期,然后在该日期上减去并加上 7 天以寻找同一工作日。它会选择最接近日期的日期。
function getNextYearWeekday(today)
{
var weekday = today.getDay();
var temp = new Date();
temp.setYear(today.getFullYear() + 1);
var previous = 0;
var next = 0;
// Check Previous Days
for (var i = 0; i < 7; i++)
{
temp.setDate(today.getDate() - i);
console.log(temp);
if (weekday == temp.getDay())
{
previous = i;
}
}
// reset the date
temp = new Date();
temp.setYear(today.getFullYear() + 1);
// Check Next Days
for (var i = 0; i < 7; i++)
{
temp.setDate(today.getDate() + i);
console.log(temp);
if (weekday == temp.getDay())
{
next = i;
}
}
// reset the date
temp = new Date();
temp.setYear(today.getFullYear() + 1);
// find out which one is closer
if (previous < next)
temp.setDate(temp.getDate() - previous);
else
temp.setDate(temp.getDate() + next);
return temp;
}
如果你想增加一年并保持日期不变,你可以将结果日期调整到同一天最近的日期,但是需要多少天,例如
function sameDayNextYear(d) {
// Copy date
var t = new Date(+d);
// Get current day number
var startDayNum = d.getDay();
// Add one year
t.setFullYear(t.getFullYear() + 1);
// Move to closest day that is the same day number
var diff = startDayNum - t.getDay();
// If diff is more than 3 days, need to subtract rather than add
diff = (diff > 3) ? diff-7 : diff;
t.setDate(t.getDate() + diff);
return t;
}
[new Date(2016, 1, 13), // Sat 13 Feb 2016 - Sat 11 Feb 2017
new Date(2016, 1, 5), // Fri 05 Feb 2016 - Fri 03 Feb 2017
new Date(2016, 1, 6), // Sat 06 Feb 2016 - Sat 04 Feb 2017
new Date(2016, 1, 29), // Mon 29 Feb 2016 - Mon 27 Feb 2017
new Date(2016, 2, 1)] // Tue 01 Mar 2016 - Tue 28 Feb 2017
.forEach(function(d) {
document.write(d + '<br>' + sameDayNextYear(d) + '<br><br>');
});
以上内容适用于任何年份的任何一天,闰日或其他日期。
但是,对于某些日期,这意味着它将转到下个月或上个月(例如 2016-03-01 转到 2017-02-28)。如果您不希望这种情况发生,您可以比较开始月份和结束月份,然后增加或减少一周以返回到原始月份(即 2017-03-07)。
我在计算明年给定日期的星期几时遇到了一些麻烦。例如:星期二,19。2016 年 4 月是给定的日期。现在我会计算:星期二,2017 年 4 月 18 日。工作日始终相同是非常重要的。
我的问题是闰年。我希望你能帮助我。
$('.changeYear').click(function() {
var appointments = [];
for (var i = 1; i <= 22; i++) {
var date = $('#tr-fix-tag-' + i).val();
if (date !== "") {
var splittedDate = date.substr(5).split('.');
appointments.push(new Date(splittedDate[2], splittedDate[1] - 1, splittedDate[0]));
}
}
var counter = 1;
$.each(appointments, function() {
var field = $('#tr-fix-tag-' + counter);
var date = $(this)[0];
var newDate = new Date(date.setFullYear(date.getFullYear() + 1));
if (isSchaltjahr(newDate.getFullYear()))
field.val(formatDate(new Date(newDate.setDate(newDate.getDate() - 2))));
else
field.val(formatDate(new Date(newDate.setDate(newDate.getDate() - 1))));
counter++;
});
});
我会用这个,然后朝着你的目标日期的方向走:
var DayOfWeek=[
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
function GetDayOfWeek(year, month, day)
{
var monthAdjust = [ 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 ];
year -= (month < 3) ? 1 : 0;
var leap = Math.floor(year / 4);
var greg = Math.floor(year / 100);
var modern = Math.floor(year / 400);
return DayOfWeek[(year + leap - greg + modern + monthAdjust[month - 1] + day) % 7];
}
这是我让它工作的最快方法。这可能会被优化很多。
这是一个DEMOhttps://jsfiddle.net/92fLoryo/
它基本上是获取明年的日期,然后在该日期上减去并加上 7 天以寻找同一工作日。它会选择最接近日期的日期。
function getNextYearWeekday(today)
{
var weekday = today.getDay();
var temp = new Date();
temp.setYear(today.getFullYear() + 1);
var previous = 0;
var next = 0;
// Check Previous Days
for (var i = 0; i < 7; i++)
{
temp.setDate(today.getDate() - i);
console.log(temp);
if (weekday == temp.getDay())
{
previous = i;
}
}
// reset the date
temp = new Date();
temp.setYear(today.getFullYear() + 1);
// Check Next Days
for (var i = 0; i < 7; i++)
{
temp.setDate(today.getDate() + i);
console.log(temp);
if (weekday == temp.getDay())
{
next = i;
}
}
// reset the date
temp = new Date();
temp.setYear(today.getFullYear() + 1);
// find out which one is closer
if (previous < next)
temp.setDate(temp.getDate() - previous);
else
temp.setDate(temp.getDate() + next);
return temp;
}
如果你想增加一年并保持日期不变,你可以将结果日期调整到同一天最近的日期,但是需要多少天,例如
function sameDayNextYear(d) {
// Copy date
var t = new Date(+d);
// Get current day number
var startDayNum = d.getDay();
// Add one year
t.setFullYear(t.getFullYear() + 1);
// Move to closest day that is the same day number
var diff = startDayNum - t.getDay();
// If diff is more than 3 days, need to subtract rather than add
diff = (diff > 3) ? diff-7 : diff;
t.setDate(t.getDate() + diff);
return t;
}
[new Date(2016, 1, 13), // Sat 13 Feb 2016 - Sat 11 Feb 2017
new Date(2016, 1, 5), // Fri 05 Feb 2016 - Fri 03 Feb 2017
new Date(2016, 1, 6), // Sat 06 Feb 2016 - Sat 04 Feb 2017
new Date(2016, 1, 29), // Mon 29 Feb 2016 - Mon 27 Feb 2017
new Date(2016, 2, 1)] // Tue 01 Mar 2016 - Tue 28 Feb 2017
.forEach(function(d) {
document.write(d + '<br>' + sameDayNextYear(d) + '<br><br>');
});
以上内容适用于任何年份的任何一天,闰日或其他日期。
但是,对于某些日期,这意味着它将转到下个月或上个月(例如 2016-03-01 转到 2017-02-28)。如果您不希望这种情况发生,您可以比较开始月份和结束月份,然后增加或减少一周以返回到原始月份(即 2017-03-07)。