扩展了与周次、月份相关的操作方法。做日历控件时,为日程信息的呈现做个月视图、周视图、日视图等。涉及到月、周相关的计算操作,就扩展Date类。
一般的计算已经够用了。

/**//**
* 扩展Date类
* 主要扩展对周次、月份的计算方法等。
*/

///
// 方法:取得日期信息
// 返回:{year:年, month:月, date:日, hours:时, minutes:分, seconds:秒, milliseconds:毫秒}
///

Date.prototype.getInfo = function()
{
//取得年、月、日、时、分、秒、毫秒
var year = this.getFullYear().toString();
var month = (this.getMonth()+1).toString();
var date = this.getDate().toString();
var hours = this.getHours().toString();
var minutes = this.getMinutes().toString();
var seconds = this.getSeconds().toString();
var milliseconds = this.getMilliseconds().toString();
//格式化年、月、日、时、分、秒、毫秒
month = (month.length==1) ? "0"+month : month;
date = (date.length==1) ? "0"+date : date;
hours = (hours.length==1) ? "0"+hours : hours;
minutes = (minutes.length==1) ? "0"+minutes : minutes;
seconds = (seconds.length==1) ? "0"+seconds : seconds;
milliseconds = (milliseconds.length==2) ? "0"+milliseconds : milliseconds;
milliseconds = (milliseconds.length==1) ? "00"+milliseconds : milliseconds;


return
{year:year,
month:month,
date:date,
hours:hours,
minutes:minutes,
seconds:seconds,
milliseconds:milliseconds};
}

///
// 方法:取得周次
// 返回:{year: 年份, zhouci: 周次}
///

Date.prototype.getZhouCi = function()
{
//本年度最后一周的情况
//如果最后一周中存在下年的日期时,作为下年第一周返回。

if (this.getMonth() == 11 && (this.getDate()-this.getDay()) > 25)
{

return
{year:this.getFullYear()+1, zhouci:1};

}else
{
//本年第一天的日期对象
var yearFirstDay = new Date(this.getFullYear(), 0, 1);
//今日是本年的第几天
var interval = Math.floor((this.valueOf() - yearFirstDay.valueOf())/1000/3600/24); //天
//周次
var zhouci = Math.floor((interval + yearFirstDay.getDay())/7) + 1;

return
{year:this.getFullYear(), zhouci:zhouci};
}
};

///
// 方法:设置周次
// 参数:increment -- 增量
///

Date.prototype.setZhouCi = function(increment)
{
//需增加的天数
var interval = parseInt(increment,10)*7; //天
//增加天数
this.setDate(this.getDate() + interval);
};

///
// 方法:本周第一天
// 返回:日期对象
///

Date.prototype.weekFirstDay = function()
{
//需增加的毫秒数
var interval = this.getDay()*24*3600*1000*-1; //毫秒
//本周第一天
var date = new Date(this.valueOf() + interval);
return date;
};

///
// 方法:本周最后一天
// 返回:日期对象
///

Date.prototype.weekLastDay = function()
{
//需增加的毫秒数
var interval = (6 - this.getDay())*24*3600*1000; //毫秒
//本周最后一天
var date = new Date(this.valueOf() + interval);
return date;
};

///
// 方法:本月第一天
// 返回:日期对象
///

Date.prototype.monthFirstDay = function()
{
//需增加的毫秒数
var interval = (this.getDate()-1)*24*3600*1000*-1; //毫秒
//本月第一天
var date = new Date(this.valueOf() + interval);
return date;
};

///
// 方法:本月最后一天
// 返回:日期对象
///

Date.prototype.monthLastDay = function()
{
//下月今日
var date = new Date(this.valueOf());
date.setMonth(date.getMonth() + 1);
//上月最后一天
date.setDate(0);
return date;
};

///
// 方法:覆盖toString()方法
///

Date.prototype.toString = function()
{
var info = this.getInfo();
return info.year + "-" +
info.month + "-" +
info.date + " " +
info.hours + ":" +
info.minutes + ":" +
info.seconds + " " +
info.milliseconds;
};