function Initcalendar () {
var nstr=new Date (); Current date information
var ynow=nstr.getfullyear (); Year
var mnow=nstr.getmonth (); Month
var dnow=nstr.getdate (); Today's date
var n1str=new Date (ynow,mnow,1); Date information for the first day of the month
var firstday=n1str.getday (); The first day of the month of the week
var m_days=new Array (31,28+is_leap (Ynow), 31,30,31,30,31,31,30,31,30,31); Total days in each month
var Tr_str=math.ceil ((M_days[mnow] + firstday)/7); Number of rows required for the table
Print the first row of the table (with a weekday flag)
var html= "<table align= ' center ' width= ' cellspacing= ' 0 ' ><tr><td align= ' center ' > Day </td> <TD align= ' center ' > A </td><td align= ' center ' > Two </td><td align= ' center ' > Three </td> <TD align= ' center ' > Four </td><td align= ' center ' > Five </td><td align= ' center ' > Six </td> </tr> ";
for (i = 0; i < tr_str; i++) {//Table rows
HTML + = "<tr>";
for (k = 0; k < 7; k++) {//table cells per row
IDX = i * 7 + K; Cell Natural Serial Number
DATE_STR = Idx-firstday + 1; Calculate date
(date_str<=0 | | date_str>m_days[mnow])? Date_str = " "
: Date_str = idx-firstday + 1; Filter invalid date (less than or equal to zero, greater than the total number of months)
Print Date: Today the background is red
Date_str = = Dnow? HTML + = "<td align= ' center ' class= ' Sign_have ' >"
+ date_str + "</td>"
: HTML + = "<td align= ' center ' >" + date_str + "</td>";
}
HTML + = "</tr>"; End of line of table
}
HTML + = "</table>"; End of table
$ ("#calendar"). HTML (HTML);
}
function Is_leap (year) {
return (Year% = = 0? res = (Year% 400 = = 0? 1:0)
: res = (year% 4 = = 0? 1:0));
}//is a leap year
JS Write a simple calendar