When reading "Javascript practice", Chapter 3 describes how to create a package. There is a datetime package which provides two methods: one is to determine whether a year is a leap year, determines the number of days in a month and considers the leap year. let's take a look at the source code:
The Code is as follows:
/**
* Jscript. datetime package
* This package contains utility functions for working with dates and times.
*/
/* Namespace */
If (typeof jscript = 'undefined '){
Jscript = function (){}
}
Jscript. datetime = function (){}
/**
* This function will return the number of days in a given month and year,
* Taking leap years into account. (This function returns the number of days for a certain year or month, and takes the leap year into consideration)
*
* @ Param inMonth The month, where January = 1 and December = 12.
* @ Param inYear The year to check the month in.
* @ Return The number of days in the specified month and year.
*/
Jscript. datetime. getNumberDaysInMonth = function (inMonth, inYear ){
InMonth = inMonth-1;
Var leap_year = this. isLeapYear (inYear );
If (leap_year ){
Leap_year = 1;
} Else {
Leap_year = 0;
}
/* 4, 6, 9, November is 30 days. Note that inMonth = inMonth-1 */
If (inMonth = 3 | inMonth = 5 | inMonth = 8 | inMonth = 10 ){
Return 30;
} Else if (inMonth = 1) {/* February is 28 or 29 days, depending on whether it is a leap year */
Return 28 + leap_year;
} Else {/* Other months are 31 days */
Return 31;
}
} // End getNumberDaysInMonth ().
/**
* This function will determine if a given year is a leap year.
* (This function is used to determine whether it is a leap year)
* @ Param inYear The year to check.
* @ Return True if inYear is a leap year, false if not.
*/
Jscript. datetime. isLeapYear = function (inYear ){
If (inYear % 4 = 0 &&! (InYear % 100 = 0) | inYear % 400 = 0 ){
Return true;
} Else {
Return false;
}
} // End isLeapYear ().