Skip to content

Instantly share code, notes, and snippets.

@williammalo
Created June 25, 2012 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williammalo/2992032 to your computer and use it in GitHub Desktop.
Save williammalo/2992032 to your computer and use it in GitHub Desktop.
Javascript Calendar Maker
//function
calendar = function(year,month,template,rowStart,rowEnd,cellStart,cellEnd){
var tmpl=function(a,b){return a.replace(/\{\{([^{}]+)}}/g,function(c,d){return typeof b[d]=="function"?b[d]():b[d]})}
return tmpl(template,{
monthName:"January,February,March,April,May,June,July,August,September,October,November,December".split(",")[month],
year:year,
days:function(f,txt,i){
f = (new Date(year,month,1)).getDay()+1
txt = ""
for(i=1;i<[31,(year%4?28:29),31,30,31,30,31,31,30,31,30,31][month]+f;i++){
txt+=(i%7==1?rowStart:"")+cellStart+(i<f?"":i-f+1)+cellEnd+(i%7==7?rowEnd:"");
}
return txt
}
});
}
//example
document.body.innerHTML=calendar(
(new Date).getYear()+1900,
(new Date).getMonth(),
"\
<table> \
<th colspan=7>{{monthName}} {{year}}</th> \
<tr> \
<td>Sun</td> \
<td>Mon</td> \
<td>Tue</td> \
<td>Wed</td> \
<td>Thu</td> \
<td>Fri</td> \
<td>Sat</td> \
</tr> \
{{days}} \
</table> \
",
"<tr>","</tr>","<td>","</td>"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment