buyizz

博客园 首页 新随笔 联系 订阅 管理

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

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

  5
  6///
  7// 方法:取得日期信息
  8// 返回:{year:年, month:月, date:日, hours:时, minutes:分, seconds:秒, milliseconds:毫秒}
  9///
 10Date.prototype.getInfo = function() {
 11    //取得年、月、日、时、分、秒、毫秒
 12    var year  = this.getFullYear().toString();
 13    var month = (this.getMonth()+1).toString();
 14    var date  = this.getDate().toString();
 15    var hours   = this.getHours().toString();
 16    var minutes = this.getMinutes().toString();
 17    var seconds = this.getSeconds().toString();
 18    var milliseconds = this.getMilliseconds().toString();
 19    
 20    //格式化年、月、日、时、分、秒、毫秒
 21    month = (month.length==1? "0"+month : month;
 22    date = (date.length==1? "0"+date : date;
 23    hours = (hours.length==1? "0"+hours : hours;
 24    minutes = (minutes.length==1? "0"+minutes : minutes;
 25    seconds = (seconds.length==1? "0"+seconds : seconds;
 26    milliseconds = (milliseconds.length==2? "0"+milliseconds : milliseconds;
 27    milliseconds = (milliseconds.length==1? "00"+milliseconds : milliseconds;
 28
 29    return {year:year, 
 30            month:month, 
 31            date:date, 
 32            hours:hours, 
 33            minutes:minutes, 
 34            seconds:seconds, 
 35            milliseconds:milliseconds}
;
 36}

 37
 38///
 39// 方法:取得周次
 40// 返回:{year: 年份, zhouci: 周次}
 41///
 42Date.prototype.getZhouCi = function() {
 43    //本年度最后一周的情况
 44    //如果最后一周中存在下年的日期时,作为下年第一周返回。
 45    if (this.getMonth() == 11 && (this.getDate()-this.getDay()) > 25{
 46        return {year:this.getFullYear()+1, zhouci:1};
 47    }
else{
 48        //本年第一天的日期对象
 49        var yearFirstDay = new Date(this.getFullYear(), 01);
 50        
 51        //今日是本年的第几天
 52        var interval = Math.floor((this.valueOf() - yearFirstDay.valueOf())/1000/3600/24); //
 53        
 54        //周次
 55        var zhouci = Math.floor((interval + yearFirstDay.getDay())/7) + 1;
 56        
 57        return {year:this.getFullYear(), zhouci:zhouci};
 58    }

 59}
;
 60
 61///
 62// 方法:设置周次
 63// 参数:increment -- 增量
 64///
 65Date.prototype.setZhouCi = function(increment) {
 66    //需增加的天数
 67    var interval = parseInt(increment,10)*7//
 68    
 69    //增加天数
 70    this.setDate(this.getDate() + interval);
 71}
;
 72
 73///
 74// 方法:本周第一天
 75// 返回:日期对象
 76///
 77Date.prototype.weekFirstDay = function() {
 78    //需增加的毫秒数
 79    var interval = this.getDay()*24*3600*1000*-1//毫秒
 80    
 81    //本周第一天
 82    var date = new Date(this.valueOf() + interval);
 83    
 84    return date;
 85}
;
 86
 87///
 88// 方法:本周最后一天
 89// 返回:日期对象
 90///
 91Date.prototype.weekLastDay = function() {
 92    //需增加的毫秒数
 93    var interval = (6 - this.getDay())*24*3600*1000//毫秒
 94    
 95    //本周最后一天
 96    var date = new Date(this.valueOf() + interval);
 97    
 98    return date;
 99}
;
100
101///
102// 方法:本月第一天
103// 返回:日期对象
104///
105Date.prototype.monthFirstDay = function() {
106    //需增加的毫秒数
107    var interval = (this.getDate()-1)*24*3600*1000*-1//毫秒
108    
109    //本月第一天
110    var date = new Date(this.valueOf() + interval);
111    
112    return date;
113}
;
114
115///
116// 方法:本月最后一天
117// 返回:日期对象
118///
119Date.prototype.monthLastDay = function() {
120    //下月今日
121    var date = new Date(this.valueOf());
122    date.setMonth(date.getMonth() + 1);
123    
124    //上月最后一天
125    date.setDate(0);
126    return date;
127}
;
128
129///
130// 方法:覆盖toString()方法
131///
132Date.prototype.toString = function() {
133    var info = this.getInfo();    
134    return info.year + "-" + 
135           info.month + "-" + 
136           info.date + " " + 
137           info.hours + ":" + 
138           info.minutes + ":" + 
139           info.seconds + " " + 
140           info.milliseconds;
141}
;
142
143
144
posted on 2008-01-11 18:55  buyizz  阅读(125)  评论(0)    收藏  举报