JS Write a simple calendar

Source: Internet
Author: User

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> &LT;TD align= ' center ' > A </td><td align= ' center ' > Two </td><td align= ' center ' > Three </td> &LT;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 = "&nbsp;"
: 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.