本文介紹了使用 Javascript 查找每月的第二個和第四個星期二的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我的協會在每個月的第二個和第四個星期二舉行會議,我非常想要一個 Javascript 代碼,這樣我就不必每兩周更新一次我們的網站,并且可以計算和顯示下一次會議的日期自動.
My association has meetings on the second and fourth Tuesday of each month and I'd really like a Javascript code so I don't have to update our site every two weeks and the date of the next meeting can be calculated and displayed automatically.
我非常感謝一些幫助,因為我完全沒有 Javascript 方面的技能,只有 CSS 和 HTML.
I'd really appreciate some help, as I have absolutely no skils in Javascript, only in CSS and HTML.
推薦答案
這是一個解決方案
HTML
<ul id="list"></ul>
Javascript
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
更新:為您提供進一步的演示
Update: a further demonstration for you
Javascript
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
這篇關于使用 Javascript 查找每月的第二個和第四個星期二的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!