当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Javascript下判断是否为闰年的Datetime包

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

2019年07月29日  | 移动技术网IT编程  | 我要评论
来看看源码:
复制代码 代码如下:

/**
* 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().

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网