使用Javascript查找本月的第二个和第四个星期二 [英] Finding the second and fourth Tuesday of the month with Javascript
本文介绍了使用Javascript查找本月的第二个和第四个星期二的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的协会在每个月的第二和第四个星期二有会议,我真的很喜欢一个Javascript代码,所以我不需要每两周更新我们的网站,下一次会议的日期可以计算和显示
我真的很感谢一些帮助,因为我完全没有Javascript,只有在CSS和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
更新:为您进一步示范
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(我们的下一次会议是:+ moment(下一个).format(MMMM Do YYYY));
在 jsfiddle
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.
I'd really appreciate some help, as I have absolutely no skils in Javascript, only in CSS and HTML.
解决方案This is a solution
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);
On 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"));
On jsfiddle
这篇关于使用Javascript查找本月的第二个和第四个星期二的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文