/*
* author:MaoFangyun
* createTime:2014-03-15
* description:在Date函数原型上进行扩展,将日期格式化
* e.g. new Date().format('yyyy-MM-dd') new Date().format('yyyy年MM月dd日 星期ww')
*/
Date.prototype.format = function(format) {
    var week = ["日", "一", "二", "三", "四", "五", "六"];
    var date = {
        "M+": this.getMonth() + 1, //月
        "d+": this.getDate(), //日
        "h+": this.getHours(), //时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S+": this.getMilliseconds(), //毫秒
        "w+": week[this.getDay()], //星期
        "f+": "01", //月初
        "l+": new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate() //月最后一天
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, k == 'w+' ? date[k] : (RegExp.$1.length == 1? date[k] : ("00" + date[k]).substr(("" + date[k]).length)));
        }
    }
    return format;
};
Date.prototype.ConvertToDate = function(date) {
    return new Date(date.replace(/-/g, '/'));
};
Date.prototype.addDays = function(d) {
    this.setDate(this.getDate() + d);
    return this;
};
Date.prototype.addWeeks = function(w) {
    this.addDays(w * 7);
    return this;
};
Date.prototype.addMonths = function(m) {
    var d = this.getDate();
    this.setMonth(this.getMonth() + m);
    if (this.getDate() < d)
        this.setDate(0);
    return this;
};
Date.prototype.addYears = function(y) {
    var m = this.getMonth();
    this.setFullYear(this.getFullYear() + y);
    if (m < this.getMonth()) {
        this.setDate(0);
    }
    return this;
};
Date.prototype.dateDiff = function(interval, endTime) {
    switch (interval) {
        case "s":   //计算秒差                        
            return parseInt((endTime - this) / 1000, 10);
        case "n":   //计算分差
            return parseInt((endTime - this) / 60000, 10);
        case "h":   //计算時差
            return parseInt((endTime - this) / 3600000, 10);
        case "d":   //计算日差
            return parseInt((endTime - this) / 86400000, 10);
        case "w":   //计算周差
            return parseInt((endTime - this) / (86400000 * 7), 10);
        case "m":   //计算月差                 
            return (endTime.getMonth() + 1) + ((endTime.getFullYear() - this.getFullYear()) * 12) - (this.getMonth() + 1);
        case "y":   //计算年差                       
            return endTime.getFullYear() - this.getFullYear();
        default:    //输入有误                      
            return undefined;
    }
}; 