js实现的日期操作类DateTime函数代码


Posted in Javascript onMarch 16, 2010
方法注解:
将指定的天数加到此实例的值上。
将指定的小时数加到此实例的值上。
将指定的分钟数加到此实例的值上。
将指定的毫秒数加到此实例的值上。
将指定的月份数加到此实例的值上。
将指定的秒数加到此实例的值上。
将指定的年份数加到此实例的值上。
将此实例的值与指定的 Date 值相比较,并指示此实例是早于、等于还是晚于指定的 Date 值。
返回一个数值相同的新DateTime对象
返回一个值,该值指示此实例是否与指定的 DateTime 实例相等。
获取此实例的日期部分。
获取此实例所表示的日期为该月中的第几天。
获取此实例所表示的日期是星期几。
获取此实例所表示日期的小时部分。
获取此实例所表示日期的分钟部分。
获取此实例所表示日期的毫秒部分。
获取此实例所表示日期的月份部分。
获取此实例的下个月一日的DateTime对象
获取此实例的下一个周日的DateTime对象
获取此实例的下一个周日的DateTime对象
获取此实例所表示日期的秒部分。
返回此实例的Date值
获取此实例所表示日期的年份部分。
指示此实例是否是DateTime对象
将当前 DateTime 对象的值转换为其等效的短日期字符串表示形式。
将当前 DateTime 对象的值转换为其等效的短时间字符串表示形式。
将当前 DateTime 对象的值转换为其等效的字符串表示形式。
验证Add系列的方法参数是否合法
继承自Date的方法
比较 DateTime 的两个实例,并返回它们相对值的指示。
返回指定年和月中的天数。
返回一个值,该值指示 DateTime 的两个实例是否相等。
返回指定的年份是否为闰年的指示。
获取一个 DateTime 对象,该对象设置为此计算机上的当前日期和时间,表示为本地时间。
将日期和时间的指定字符串表示形式转换为其等效的 DateTime。
获取当前日期,其时间组成部分设置为 00:00:00。
//表示时间上的一刻,通常以日期和当天的时间表示。 
function DateTime(year, month, day, hour, min, sec, millisec){ 
    var d = new Date();     if (year || year == 0){ 
        d.setFullYear(year); 
    } 
    if (month || month == 0){ 
        d.setMonth(month - 1); 
    } 
    if (day || day == 0){ 
        d.setDate(day); 
    } 
    if (hour || hour == 0){ 
        d.setHours(hour); 
    } 
    if (min || min == 0){ 
        d.setMinutes(min); 
    } 
    if (sec || sec == 0){ 
        d.setSeconds(sec); 
    } 
    if (millisec || millisec == 0){ 
        d.setMilliseconds(millisec); 
    } 
    //将指定的天数加到此实例的值上。 
    this.AddDays = function(value){ 
        if(!ValidateAddMethodParam(value)){ 
            return null; 
        } 
        var result = this.Clone(); 
        result.GetValue().setDate(result.GetDay() + value); 
        return result; 
    } 
    //将指定的小时数加到此实例的值上。 
    this.AddHours = function(value){ 
        if(!ValidateAddMethodParam(value)){ 
            return null; 
        } 
        var result = this.Clone(); 
        result.GetValue().setHours(result.GetHour() + value); 
        return result; 
    } 
    //将指定的分钟数加到此实例的值上。 
    this.AddMinutes = function(value){ 
        if(!ValidateAddMethodParam(value)){ 
            return null; 
        } 
        var result = this.Clone(); 
        result.GetValue().setMinutes(result.GetMinute() + value); 
        return result; 
    } 
    //将指定的毫秒数加到此实例的值上。 
    this.AddMilliseconds = function(value){ 
        if(!ValidateAddMethodParam(value)){ 
            return null; 
        } 
        var result = this.Clone(); 
        result.GetValue().setMilliseconds(result.GetMillisecond() + value); 
        return result; 
    } 
    //将指定的月份数加到此实例的值上。 
    this.AddMonths = function(value){ 
        if(!ValidateAddMethodParam(value)){ 
            return null; 
        } 
        var result = this.Clone(); 
        result.GetValue().setMonth(result.GetValue().getMonth() + value); 
        return result; 
    } 
    //将指定的秒数加到此实例的值上。 
    this.AddSeconds = function(value){ 
        if(!ValidateAddMethodParam(value)){ 
            return null; 
        } 
        var result = this.Clone(); 
        result.GetValue().setSeconds(result.GetSecond() + value); 
        return result; 
    } 
    //将指定的年份数加到此实例的值上。 
    this.AddYears = function(value){ 
        if(!ValidateAddMethodParam(value)){ 
            return null; 
        } 
        var result = this.Clone(); 
        result.GetValue().setFullYear(result.GetYear() + value); 
        return result; 
    }     
    //将此实例的值与指定的 Date 值相比较,并指示此实例是早于、等于还是晚于指定的 Date 值。 
    this.CompareTo = function(other){ 
        var internalTicks = other.getTime(); 
        var num2 = d.getTime(); 
     if (num2 > internalTicks) 
     { 
     return 1; 
     } 
     if (num2 < internalTicks) 
     { 
     return -1; 
     } 
     return 0; 
    } 
    //返回一个数值相同的新DateTime对象 
    this.Clone = function(){ 
        return new DateTime( 
             this.GetYear() 
            ,this.GetMonth() 
            ,this.GetDay() 
            ,this.GetHour() 
            ,this.GetMinute() 
            ,this.GetSecond() 
            ,this.GetMillisecond()); 
    } 
    //返回一个值,该值指示此实例是否与指定的 DateTime 实例相等。 
    this.Equals = function(other){ 
        return this.CompareTo(other) == 0; 
    } 
    //获取此实例的日期部分。 
    this.GetDate = function(){ 
        var result = new DateTime(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0, 0); 
        return result ; 
    } 
    //获取此实例所表示的日期为该月中的第几天。 
    this.GetDay = function(){ 
        return d.getDate(); 
    } 
    //获取此实例所表示的日期是星期几。 
    this.GetDayOfWeek = function(){ 
        return d.getDay(); 
    } 
    //获取此实例所表示日期的小时部分。 
    this.GetHour = function(){ 
        return d.getHours(); 
    } 
    //获取此实例所表示日期的分钟部分。 
    this.GetMinute = function(){ 
        return d.getMinutes(); 
    } 
    //获取此实例所表示日期的毫秒部分。 
    this.GetMillisecond = function(){ 
        return d.getMilliseconds(); 
    } 
    //获取此实例所表示日期的月份部分。 
    this.GetMonth = function(){ 
        return d.getMonth() + 1; 
    } 
    //获取此实例的下个月一日的DateTime对象 
    this.GetNextMonthFirstDay = function(){ 
        var result = new DateTime(this.GetYear(), this.GetMonth(), 1, 0, 0, 0, 0); 
        result = result.AddMonths(1); 
        return result; 
    } 
    //获取此实例的下一个周日的DateTime对象 
    this.GetNextWeekFirstDay = function(){ 
        var result = this.GetDate(); 
        return result.AddDays(7 - result.GetDayOfWeek()); 
    } 
    //获取此实例的下一个周日的DateTime对象 
    this.GetNextYearFirstDay = function(){ 
        return new DateTime(this.GetYear() + 1, 1, 1, 0, 0, 0, 0); 
    } 
    //获取此实例所表示日期的秒部分。 
    this.GetSecond = function(){ 
        return d.getSeconds(); 
    } 
    //返回此实例的Date值 
    this.GetValue = function(){ 
        return d; 
    } 
    //获取此实例所表示日期的年份部分。 
    this.GetYear = function(){ 
        return d.getFullYear(); 
    } 
    //指示此实例是否是DateTime对象 
    this.IsDateTime = function(){} 
    //将当前 DateTime 对象的值转换为其等效的短日期字符串表示形式。 
    this.ToShortDateString = function(){ 
        var result = ""; 
        result = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate(); 
        return result;     
    } 
    //将当前 DateTime 对象的值转换为其等效的短时间字符串表示形式。 
    this.ToShortTimeString = function(){ 
        var result = ""; 
        result = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); 
        return result;     
    } 
    //将当前 DateTime 对象的值转换为其等效的字符串表示形式。 
    this.ToString = function(format){ 
        if(typeof(format) == "string"){ 
        } 
        return this.ToShortDateString() + " " + this.ToShortTimeString(); 
    } 
    //验证Add系列的方法参数是否合法 
    function ValidateAddMethodParam(param){ 
        if(typeof(param) != "number"){ 
            return false; 
        } 
        return true; 
    } 
    //继承自Date的方法 
    this.getTime = function(){ 
        return d.getTime(); 
    } 
} 
//比较 DateTime 的两个实例,并返回它们相对值的指示。 
DateTime.Compare = function(d1, d2){ 
    return d1.CompareTo(d2); 
} 
//返回指定年和月中的天数。 
DateTime.DaysInMonth = function(year, month){ 
if ((month < 1) || (month > 12)) 
{ 
return "月份[" + month + "]超出范围"; 
} 
var numArray = DateTime.IsLeapYear(year) ? DateTime.DaysToMonth366 : DateTime.DaysToMonth365; 
return (numArray[month] - numArray[month - 1]); 
} 
//返回一个值,该值指示 DateTime 的两个实例是否相等。 
DateTime.Equals = function(d1, d2){ 
    return d1.CompareTo(d2) == 0; 
} 
//返回指定的年份是否为闰年的指示。 
DateTime.IsLeapYear = function(year) 
{ 
if ((year < 1) || (year > 0x270f)) 
{ 
return "年份[" + year + "]超出范围"; 
} 
if ((year % 4) != 0) 
{ 
return false; 
} 
if ((year % 100) == 0) 
{ 
return ((year % 400) == 0); 
} 
return true; 
} 
//获取一个 DateTime 对象,该对象设置为此计算机上的当前日期和时间,表示为本地时间。 
DateTime.Now = new DateTime(); 
//将日期和时间的指定字符串表示形式转换为其等效的 DateTime。 
DateTime.Parse = function(s){ 
    var result = new DateTime(); 
    var value = result.GetValue(); 
    value.setHours(0,0,0,0); 
    var dateRex = /\b[1-2][0-9][0-9][0-9][-]\d{1,2}[-]\d{1,2}\b/i; 
    if(dateRex.test(s)){ 
        var dateStr = s.match(dateRex)[0]; 
        try{ 
            var dateParts = dateStr.split("-"); 
            var year = dateParts[0] - 0; 
            var month = dateParts[1] - 1; 
            var day = dateParts[2] - 0; 
            value.setFullYear(year,month,day); 
        }catch(ex){ 
            return null; 
        } 
        var timeRex = /\b\d{1,2}[:]\d{1,2}[:]\d{1,2}\b/i; 
        if(timeRex.test(s)){ 
            var timeStr = s.match(timeRex)[0]; 
            try{ 
                var timeParts = timeStr.split(":"); 
                var hour = timeParts[0] - 0; 
                var min = timeParts[1] - 0; 
                var sec = timeParts[2] - 0; 
                value.setHours(hour,min,sec); 
            }catch(ex){ 
            } 
        } 
    }else{ 
        return null; 
    } 
    return result; 
} 
//获取当前日期,其时间组成部分设置为 00:00:00。 
DateTime.Today = new DateTime(null, null, null, 0, 0, 0, 0); 
//静态字段 
DateTime.DaysToMonth365 = [ 0, 0x1f, 0x3b, 90, 120, 0x97, 0xb5, 0xd4, 0xf3, 0x111, 0x130, 0x14e, 0x16d ]; 
DateTime.DaysToMonth366 = [ 0, 0x1f, 60, 0x5b, 0x79, 0x98, 0xb6, 0xd5, 0xf4, 0x112, 0x131, 0x14f, 0x16e ];
Javascript 相关文章推荐
jsTree 基于JQuery的排序节点 Bug
Jul 26 Javascript
JavaScript中的apply()方法和call()方法使用介绍
Jul 25 Javascript
js打开新窗口方法整理
Feb 17 Javascript
AngularJS中的Directive自定义一个表格
Jan 25 Javascript
分享javascript、jquery实用代码段
Oct 20 Javascript
JavaScript函数节流和函数防抖之间的区别
Feb 15 Javascript
Vue利用canvas实现移动端手写板的方法
May 03 Javascript
用vue-cli开发vue时的代理设置方法
Sep 20 Javascript
通过实例了解js函数中参数的传递
Jun 15 Javascript
vue如何实现自定义底部菜单栏
Jul 01 Javascript
微信小程序自定义弹窗滚动与页面滚动冲突的解决方法
Jul 16 Javascript
ant design vue datepicker日期选择器中文化操作
Oct 28 Javascript
javascript json2 使用方法
Mar 16 #Javascript
jQuery 选择器理解
Mar 16 #Javascript
jQuery 学习入门篇附实例代码
Mar 16 #Javascript
Jquery Ajax学习实例4 向WebService发出请求,返回实体对象的异步调用
Mar 16 #Javascript
Jquery Ajax学习实例3 向WebService发出请求,调用方法返回数据
Mar 16 #Javascript
javascript 二维数组的实现与应用
Mar 16 #Javascript
Jquery Ajax 学习实例2 向页面发出请求 返回JSon格式数据
Mar 15 #Javascript
You might like
php 使用post,get的一种简洁方式
2010/04/25 PHP
PHP数据过滤的方法
2013/10/30 PHP
php数组排序usort、uksort与sort函数用法
2014/11/17 PHP
Laravel框架定时任务2种实现方式示例
2018/12/08 PHP
调用js时ie6和ie7,ff的区别
2009/08/19 Javascript
基于jquery实现的类似百度搜索的输入框自动完成功能
2011/08/23 Javascript
javascript实现切换td中的值
2014/12/05 Javascript
深入理解JavaScript系列(26):设计模式之构造函数模式详解
2015/03/03 Javascript
json格式数据的添加,删除及排序方法
2016/01/21 Javascript
web前端开发中常见的多列布局解决方案整理(一定要看)
2017/10/15 Javascript
node.js使用stream模块实现自定义流示例
2020/02/13 Javascript
nodeJs项目在阿里云的简单部署
2020/11/27 NodeJs
[03:10]超级美酒第四天 fy拉比克秀 大合集
2018/06/05 DOTA
Python网络爬虫实例讲解
2016/04/28 Python
Python实现自动添加脚本头信息的示例代码
2016/09/02 Python
15行Python代码带你轻松理解令牌桶算法
2018/03/21 Python
在Python中如何传递任意数量的实参的示例代码
2019/03/21 Python
python 实现创建文件夹和创建日志文件的方法
2019/07/07 Python
python实现批量修改服务器密码的方法
2019/08/13 Python
Django查询优化及ajax编码格式原理解析
2020/03/25 Python
Python基于numpy模块实现回归预测
2020/05/14 Python
Django 解决开发自定义抛出异常的问题
2020/05/21 Python
HTML5应用之文件上传
2016/12/30 HTML / CSS
html5的canvas实现3d雪花飘舞效果
2013/12/27 HTML / CSS
html5自定义video标签的海报与播放按钮功能
2019/12/04 HTML / CSS
canvas实现烟花的示例代码
2020/01/16 HTML / CSS
国家地理在线商店:Shop National Geographic
2018/06/30 全球购物
Nike澳大利亚官网:Nike.com (AU)
2019/06/03 全球购物
几道Java和数据库的面试题
2013/05/30 面试题
大学生实习思想汇报
2014/01/12 职场文书
模范家庭事迹材料
2014/02/10 职场文书
优秀辅导员事迹材料
2014/02/16 职场文书
学生旷课检讨书500字
2014/10/28 职场文书
2015新教师教学工作总结
2015/07/22 职场文书
广告文案的撰写技巧(实用干货)
2019/08/23 职场文书
浅析JavaScript中的变量提升
2022/06/01 Javascript