日期对象算法,用于返回最接近的每两周一天的日期
Date Object algorithm to return date of nearest bi-weekly day of week
目标:自动显示即将举行的双周活动的日期。在这种情况下,每个星期六。理想情况下,通过修改现有代码。
例如,如果 7 月 9 日有一个事件,而今天是 7 月 11 日,则下一个事件将是 7 月 23 日。如果今天是 7 月 24 日,下一个事件是 8 月 6 日,依此类推......
问题:如何计算日期?(我假设需要一个参考开始日期,例如 2016 年 7 月 9 日)
背景/现有代码:计算一周中最近的给定日期。(例如,如果今天是 14 日星期一,那么即将到来的星期六的日期是什么?注意:在这种情况下,活动是每周五和周六。我们显示即将到来的周五和周六的日期。但我想确保显示的日期总是在同一个周末。(如果今天是星期六,FRI 日期应该是昨天,SAT 日期应该是今天)。
(function() {
// Today's date
var startingDate = new Date();
// Find date of nearest day of week
function dateOfNearestDay(startingDate, nearestDay) {
// Date object to work with inside the function
var nearestTime = new Date(startingDate.getTime());
// If today is Sat, use last Fri's date, else calculate date of nearest day
if (startingDate.getDay() === 6 && nearestDay === 5) {
nearestTime.setDate((startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7) - 7);
}
else {
nearestTime.setDate(startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7);
}
return nearestTime;
}
// Format date as needed for display
function getMonthAndDay(date) {
// Array to store month names for translation
monthNames = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'];
return monthNames[date.getMonth()]+ " " + date.getDate();
}
// Get date object of nearest Friday and Saturday
var friday = dateOfNearestDay(startingDate, 5);
var saturday = dateOfNearestDay(startingDate, 6);
var fridayDate = document.getElementById('friday');
var saturdayDate = document.getElementById('saturday');
// Write month & day of nearest day
fridayDate.textContent = getMonthAndDay(friday);
saturdayDate.textContent = getMonthAndDay(saturday);
}());
event
的给定日期today
和已知日期的函数nextEvent
计算today
或today
后的下一个双周重复事件:
// Today's date
var startingDate = new Date();
startingDate.setDate(startingDate.getDate());
// Find date of nearest day of week
function dateOfNearestDay(startingDate, nearestDay) {
// Date object to work with inside the function
var nearestTime = new Date(startingDate.getTime());
// If today is Sat, use last Fri's date, else calculate date of nearest day
if (startingDate.getDay() === 6 && nearestDay === 5) {
nearestTime.setDate((startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7) - 7 - 7);
} else {
nearestTime.setDate(startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7 - 7);
}
return nearestTime;
}
// For a given today and last event finds closest next event from today.
// Assumption is that event is recurring every 14 days
function nextEvent(today, event) {
var eventRecurrence = 14; // recurrence of the event in days
var nextEventTime = new Date(today.getTime());
nextEventTime.setDate(today.getDate() + ((today.getDate() > event.getDate()) ? eventRecurrence : 0) - (today.getDate() - event.getDate()) % eventRecurrence);
return nextEventTime;
}
// Format date as needed for display
function getMonthAndDay(date) {
// Array to store month names for translation
monthNames = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'];
return monthNames[date.getMonth()] + " " + date.getDate();
}
// Get date object of nearest Friday and Saturday
var friday = dateOfNearestDay(startingDate, 5);
var saturday = dateOfNearestDay(startingDate, 6);
var nextFridayEvent = nextEvent(startingDate, friday);
var nextSaturdayEvent = nextEvent(startingDate, saturday);
// Write months & days of the variables
console.log('Today is ' + getMonthAndDay(startingDate));
console.log('Friday Event ' + getMonthAndDay(friday));
console.log('Saturday Event ' + getMonthAndDay(saturday));
console.log('Next Friday Event ' + getMonthAndDay(nextFridayEvent));
console.log('Next Saturday Event ' + getMonthAndDay(nextSaturdayEvent))
我已经用 2x -7
修改了您的函数dateOfNearestDay
,以便在today
之前演示事件。
相关文章:
- momentjs获取目标日期的周天数
- 在Jquery或Javascript中获取两个范围或日期之间的通信周数
- 每两周或每两个月
- 两周前在jquery中
- 如何放置日期范围从周选择器用于两个文本输入
- 显示当前日期两次
- 如何根据当前日期显示两周期间的第一天
- 如何获取两个选定月份日期之间的一周的特定日期
- 显示日期1周前&昨天日期
- 用moment.js从给定日期减去周的最佳方式是什么
- 如何在javascript中获取两个不同日期之间的周天数
- 如何在asp.net/c#中从两个开始和结束自定义日期获取周数
- 试图在Jquery中获得两个日期之间的周数
- 在javascript中使用当前日期获取两周日期
- 在javascript中获取起始和结束日期的周数
- jQuery UI Datepicker:限制为两周
- 要显示数据的最后一周和任何两个日期之间的数据,我正在努力
- Angularjs:如何获得两个日期之间的周数
- firebase有办法每两周调用我的代码吗?日程安排
- 当前日期两个时间之间的时间