教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 javascript Date类的扩展实例

javascript Date类的扩展实例

发布时间:2015-02-13   编辑:jiaochengji.com
分享一个javascript中date类的扩展,也是一个不错的学习js date类的例子,有需要的朋友参考下。

javascript中date类的扩展。
代码:
 

复制代码 代码示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>datetime-www.jiaochengji.com</title>
</head>
<body>
<script language="javascript" type="text/javascript">
Date.prototype.add = function(milliseconds){
    var m = this.getTime() + milliseconds;
    return new Date(m);
};
Date.prototype.addSeconds = function(second){
    return this.add(second * 1000);
};
Date.prototype.addMinutes = function(minute){
    return this.addSeconds(minute*60);
};
Date.prototype.addHours = function(hour){
    return this.addMinutes(60*hour);
};

Date.prototype.addDays = function(day){
    return this.addHours(day * 24);
};

Date.isLeepYear = function(year){
    return (year % 4 == 0 && year % 100 != 0)
};

Date.daysInMonth = function(year,month){
    if(month == 2){
        if(year % 4 == 0 && year % 100 != 0)
            return 29;
        else
            return 28;
    }
    else if((month <= 7 && month % 2 == 1) || (month > 7 && month % 2 == 0))
        return 31;
    else
        return 30;
};

Date.prototype.addMonth = function(){
    var m = this.getMonth();
    if(m == 11)return new Date(this.getFullYear() + 1,1,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
   
    var daysInNextMonth = Date.daysInMonth(this.getFullYear(),this.getMonth() + 1);
    var day = this.getDate();
    if(day > daysInNextMonth){
        day = daysInNextMonth;
    }
    return new Date(this.getFullYear(),this.getMonth() + 1,day,this.getHours(),this.getMinutes(),this.getSeconds());   
};

Date.prototype.subMonth = function(){
    var m = this.getMonth();
    if(m == 0)return new Date(this.getFullYear() -1,12,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
    var day = this.getDate();
    var daysInPreviousMonth = Date.daysInMonth(this.getFullYear(),this.getMonth());
    if(day > daysInPreviousMonth){
        day = daysInPreviousMonth;
    }
    return new Date(this.getFullYear(),this.getMonth() - 1,day,this.getHours(),this.getMinutes(),this.getSeconds());
};

Date.prototype.addMonths = function(addMonth){
    var result = false;
    if(addMonth > 0){
        while(addMonth > 0){
            result = this.addMonth();
            addMonth -- ;
        }
    }else if(addMonth < 0){
        while(addMonth < 0){
            result = this.subMonth();
            addMonth ++ ;
        }
    }else{
        result = this;
    }
    return result;
};

Date.prototype.addYears = function(year){
    return new Date(this.getFullYear() + year,this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
};
var d = new Date();
alert('d.addYears(2) = ' + d.addYears(2).toLocaleString());
alert('d.addMonths(2) = ' + d.addMonths(2).toLocaleString());
alert('d.addMonths(-2) = ' + d.addMonths(-2).toLocaleString());

alert('d.addDays(2) = ' + d.addDays(2).toLocaleString());
alert('d.addHours(2) = ' + d.addHours(2).toLocaleString());
alert('d.addMinutes(2) = ' + d.addMinutes(2).toLocaleString());
alert('d.addSeconds(2) = ' + d.addSeconds(2).toLocaleString());
</script>
</body>
</html>

您可能感兴趣的文章:
javascript Date类的扩展实例
扩展jquery实现客户端表格的分页、排序功能代码
怎样使用javascript Date Format方法
javascript日期对象格式化为字符串
php实例之检测可用的mysql扩展类型
Javascript 设计模式 — Singleton(单例)模式
jquery的extend和fn.extend的使用说明
js long日期格式转为标准日期格式的代码
JavaScript自定义日期格式化函数用法
php实现文件的自动ftp更新

[关闭]
~ ~