我的协会在每个月的第二个和第四个星期二开会,我真的很想使用Javascript代码,因此我不必每两周更新一次网站,下次会议的日期可以自动计算并显示。
我非常感谢您的帮助,因为我绝对没有Javascript方面的知识,只有CSS和HTML方面的知识。
最佳答案
这是一个解决方案
的HTML
<ul id="list"></ul>
Java脚本
function getTuesdays(month, year) {
var d = new Date(year, month, 1),
tuesdays = [];
d.setDate(d.getDate() + (9 - d.getDay()) % 7)
while (d.getMonth() === month) {
tuesdays.push(new Date(d.getTime()));
d.setDate(d.getDate() + 7);
}
return tuesdays;
}
var meetingTuesdays = [],
ul = document.getElementById("list"),
temp,
li,
i;
for ( i = 0; i < 12; i += 1) {
temp = getTuesdays(i, 2013);
meetingTuesdays.push(temp[1]);
li = document.createElement("li");
li.textContent = temp[1];
ul.appendChild(li);
meetingTuesdays.push(temp[3]);
li = document.createElement("li");
li.textContent = temp[3];
ul.appendChild(li);
}
console.log(meetingTuesdays);
在jsfiddle
更新:为您做的进一步示范
Java脚本
function getTuesdays(month, year) {
var d = new Date(year, month, 1),
tuesdays = [];
d.setDate(d.getDate() + (9 - d.getDay()) % 7)
while (d.getMonth() === month) {
tuesdays.push(new Date(d.getTime()));
d.setDate(d.getDate() + 7);
}
return tuesdays;
}
var today = new Date(),
theseTuesdays = getTuesdays(today.getMonth(), today.getFullYear()),
next;
theseTuesdays.some(function (tuesday, index) {
if (index % 2 === 1 && tuesday > today) {
next = tuesday;
return true;
}
return false;
});
alert("Our next meeting is on : " + moment(next).format("MMMM Do YYYY"));
在jsfiddle