javascript - How to build week data in calendar for my case? -



javascript - How to build week data in calendar for my case? -

i trying build calendar modal app using angular. have model this

i have no problem building year month not sure how setup week has other day. example, first week of oct has 28,29,30 in first week.

//code build day var month = 9; // hardcoded demo. var montha = []; (var m = 0; m <= 3; m ++) { var weeks = []; totalday = new date(year, month + m + 1, 0).getdate() //build days (var = 0; <= totalday; i++) { //setting weeks if (i % 7 == 0) { weeks.push([]); } weeks[weeks.length-1].push(i); } var monthobj = { month : (month + m), weeks:weeks } montha.push(monthobj); }

the above code produce

montha: [ { month: '10', weeks: [ [1,2,3,4,5,6,7], [8,9,10,11,12,13,14], [15,16,17,18,19,20,21], [21,22,23,24,25,26,27], [28,29,30,31] ] }, { month: 'nov', weeks: [1,2,3,4,5,6,7] ... }... ] }

i hoping

month: [ { month: '10', weeks: [ [28, 29, 30, 1,2,3,4], //i haven o problem building week 1 4 [5,6,7,8,9,10,11], [12,13,14,15,16,17,18], [19,20,21,22,23,24,25], [26,27,28,29,30,31,1] ] }, { month: '11', weeks: [26,27,28,29,30,31,1] ... } ]

thanks much help!

this function made should want:

class="snippet-code-js lang-js prettyprint-override">function getweeksinmonth(year, month){ //month 1-12 based var weeks = []; lastdayinmonth = new date(year, month, 0) // lastly day in month firstdayinmonth = new date(year, month-1, 1) //get first day in month start = new date(firstdayinmonth.gettime()); //copy firstday start.setdate(start.getdate() - start.getday() ); // set date previous sunday end=new date(lastdayinmonth.gettime()); //copy lastday end.setdate(end.getdate() - end.getday() ); // set date previous sunday if(end < lastdayinmonth){ // if previous sunday not lastday itself, want add together week end.setdate(end.getdate() + 7); } var counter=0; var week=[] for(;start<=end;start.setdate(start.getdate()+1)){ // traverse days if(counter % 7==0 && counter != 0){ // every 7 days add together week var copy=week.slice(0); weeks.push(copy) week=[]; } week.push(start.getdate()) counter++ } homecoming weeks; } var weeks = getweeksinmonth(2014,10); for(var i=0;i<weeks.length;i++){ document.body.innerhtml+=weeks[i].tostring() + '<br/>'; }

javascript angularjs calendar

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -