date - Get the correct days in a specific month in 2014 javascript -
i have problem getting correct days each month in year 2014.. shows january , february has 31 days each, , march 28. im unable fix this. maybe can come tips or tricks? :)
edit: can see programme first month == 0 collecting december year 2013 wierd, because refer 2014 in "date" function. have suggestions?
here code: months , days in swedish, dont think problem..
function daysinamonth (month) { return new date(2014, month, 0).getdate(); }  function nameoftheweekdays(day) { return [ "måndag", "tisdag", "onsdag", "torsdag", "fredag", "lördag", "söndag" ][day]; }  function getmonthname(month) {  return [  "januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december" ][month]; }  var month = 0; var day = 0;  (var i=0; i<12; i++) { (var x=0; x<daysinamonth(i); x++) {  var c = new date(2014, i, x);     document.writeln( nameoftheweekdays(c.getday()) + " " + (x+1) + " " + getmonthname(month));    day++;  }  month++; }      
months in javascript dates numbered 0 through 11, not 1 through 12. indeed, "daysinamonth()" function seems rely on fact.
the date 1 january 2014 obtained new date(2014, 0, 1).
you can use array map month or day numbers names:
function getmonthname(m) {   return [     "januari", "februari", "mars", "april",     "maj", "juni", "juli", "augusti",     "september", "oktober", "november", "december"   ][m]; }      
Comments
Post a Comment