时间: 2021-07-31 作者:daque
===编写日历一=== 当calendar.html挪用 mycal:calendar,当月的日历将会表露在页面中,因变量setcal()是重要步调段,它初始化少许变量并挪用drawcal()因变量。咱们也运用了三个其余因变量:getmonthname()、 getdays() 和 leapyear()。让咱们从结果一个因变量发端:
getdays()因变量接受哪月值和哪年值,而且创造一个有12个元素的数组,用来寄存每月的天数,哪一年用来确定是否闰年,在闰年中仲春是29天,而不是闰年是28天。该因变量归来指定月份的天数。
以次是getdays():function getdays(month, year) { // create array to hold number of days in each month var ar = new array(12); ar[0] = 31; // january ar[1] = (leapyear(year)) ? 29 : 28; // february ar[2] = 31; // march ar[3] = 30; // april ar[4] = 31; // may ar[5] = 30; // june ar[6] = 31; // july ar[7] = 31; // august ar[8] = 30; // september ar[9] = 31; // october ar[10] = 30; // november ar[11] = 31; // december // return number of days in the specified month (parameter) return ar[month]; }
即使指定的年数不妨被4整除,那么leapyear()因变量将归来“true”,要不归来”false“:
function leapyear(year) { if (year % 4 == 0) // basic rule return true; // is leap year /* else */ // else not needed when statement is "return" return false; // is not leap year } getmonthname()因变量归来指定月份的名字: function getmonthname(month) { // create array to hold name of each month var ar = new array(12); ar[0] = "january"; ar[1] = "february"; ar[2] = "march"; ar[3] = "april"; ar[4] = "may"; ar[5] = "june"; ar[6] = "july"; ar[7] = "august"; ar[8] = "september"; ar[9] = "october"; ar[10] = "november"; ar[11] = "december"; // return name of specified month (parameter) return ar[month]; }
setcal()因变量是主模块,咱们在剧本的第一条龙挪用它。该因变量为当天(now)、和每月的第一天(firstdayinstance)创造一个date东西。用那些东西,setcal()因变量领会出对于一个月的第一天、当天,和结果一天的一切消息。
function setcal() { // standard time attributes var now = new date(); var year = now.getfullyear(); var month = now.getmonth(); var monthname = getmonthname(month); var date = now.getdate(); now = null; // create instance of first day of month, and extract the day on which it occurs var firstdayinstance = new date(year, month, 1); var firstday = firstdayinstance.getday(); firstdayinstance = null; // number of days in current month var days = getdays(month, year); // call function to draw calendar drawcal(firstday + 1, days, date, monthname, year); }