0

0

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

php中文网

php中文网

发布时间:2016-05-16 18:32:06

|

1488人浏览过

|

来源于php中文网

原创

方法注解:

将指定的天数加到此实例的值上。

将指定的小时数加到此实例的值上。

将指定的分钟数加到此实例的值上。

将指定的毫秒数加到此实例的值上。

将指定的月份数加到此实例的值上。

将指定的秒数加到此实例的值上。

将指定的年份数加到此实例的值上。

将此实例的值与指定的 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      {
     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 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 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 ];

相关文章

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

js

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

8

2026.01.30

c++ 字符串格式化
c++ 字符串格式化

本专题整合了c++字符串格式化用法、输出技巧、实践等等内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.30

java 字符串格式化
java 字符串格式化

本专题整合了java如何进行字符串格式化相关教程、使用解析、方法详解等等内容。阅读专题下面的文章了解更多详细教程。

8

2026.01.30

python 字符串格式化
python 字符串格式化

本专题整合了python字符串格式化教程、实践、方法、进阶等等相关内容,阅读专题下面的文章了解更多详细操作。

1

2026.01.30

java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

20

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

17

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

19

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

3

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
WEB前端教程【HTML5+CSS3+JS】
WEB前端教程【HTML5+CSS3+JS】

共101课时 | 8.6万人学习

JS进阶与BootStrap学习
JS进阶与BootStrap学习

共39课时 | 3.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号