date - Finding the second and fourth Tuesday of the month with Javascript -
my association has meetings on second , fourth tuesday of each month , i'd javascript code don't have update our site every 2 weeks , date of next meeting can calculated , displayed automatically.
i'd appreciate help, have absolutely no skils in javascript, in css , html.
this 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; ( = 0; < 12; += 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: further demonstration 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 on : " + moment(next).format("mmmm yyyy"));
on jsfiddle
Comments
Post a Comment