欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Javascript下判断是否为闰年的Datetime包

程序员文章站 2022-05-16 11:40:02
来看看源码: 复制代码 代码如下: /** * jscript.datetime package * this package contains utility funct...
来看看源码:
复制代码 代码如下:

/**
* jscript.datetime package
* this package contains utility functions for working with dates and times.
*/
/*命名空间*/
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.(这个函数返回所给某年、某月的天数,并且考虑了闰年的情况)
*
* @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, 11 月为 30 天,注意上面的 inmonth = inmonth - 1*/
if (inmonth == 3 || inmonth == 5 || inmonth == 8 || inmonth == 10) {
return 30;
} else if (inmonth == 1) {/*2 月为 28 或者 29 天,视是否为闰年而定*/
return 28 + leap_year;
} else {/*其它月则为 31 天*/
return 31;
}

} // end getnumberdaysinmonth().


/**
* this function will determine if a given year 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().