Determine whether it is a Datetime package of a leap year in Javascript

Source: Internet
Author: User
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 ().

Related Article

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.