js中日期函数的相关操作

Date对象具有多种构造函数,下面简单列举如下:

  new Date() 
  new Date(milliseconds) 
  new Date(datestring) 
  new Date(year, month) 
  new Date(year, month, day) 
  new Date(year, month, day, hours) 
  new Date(year, month, day, hours, minutes) 
  new Date(year, month, day, hours, minutes, seconds) 
  new Date(year, month, day, hours, minutes, seconds, microseconds) 

下面对以上几个构造函数进行简单的分析:

  1.new Date(),没有参数的时候,创建的是当前时间日期对象。
  2.new Date(milliseconds),当参数为数字的时候表示毫秒数,创建一个距离1970年1月一日指定毫秒的时间日期对象。
  3.new Date(datestring),此参数是一个字符串,并且此字符串一定要能够使用Date.parse(datestring)转换。

    parse() 方法可解析一个日期时间字符串,并返回 1970年1月1日午夜距离该日期时间的毫秒数。
    语法:Date.parse(datestring)
    参数:datestring 是必需的,表示日期和时间的字符串。
    该方法是 Date 对象的静态方法。一般采用 Date.parse() 的形式来调用,而不是通过 dateobject.parse() 调用该方法。

  4.以下是其余六个构造函数的精确定义:
    1)year,是一个整数,如果是0-99,那么在此基础上加1900,其他的都原样返回。
    2)month,是一个整数,范围是0-11。
    3)day,是一个整数,范围是1-31。
    4)hours,是一个整数,范围是0-23。
    5)minutes,是一个整数,范围是0-59。
    6)seconds,是一个整数,范围是0-59。
    7)microseconds,是一个整数,范围是0-9999。

下面是js代码实例:

复制代码
  window.οnlοad=function(){
        var nowtime = new Date();//获取当前系统时间对象
        alert("nowtime:"+nowtime);
        var nowdate = nowtime.Format("yyyy-MM-dd hh:mm:ss");//格式化当前系统时间
        alert("nowdate:"+nowdate);

        var datestring = nowtime.getTime();//获取当前系统时间的时间戳
        alert("datestring:"+datestring);
        var mytime = new Date(datestring);//将时间戳转化为日期时间对象
        alert("mytime:"+mytime);
        
        var mydate = "2016-07-07 00:00:01";
        if(compareTime(mydate, nowdate)){//进行日期时间比较
            alert("指定时间没过期");
        }else{
            alert("指定时间已过期");
        }
    }

    //比较yyyy-MM-dd hh:mm:ss格式的日期时间大小
    function compareTime(startDate, endDate) {      
        var startDateTemp = startDate.split(" ");   
        var endDateTemp = endDate.split(" ");   
                       
        var arrStartDate = startDateTemp[0].split("-");   
        var arrEndDate = endDateTemp[0].split("-");   
      
        var arrStartTime = startDateTemp[1].split(":");   
        var arrEndTime = endDateTemp[1].split(":");   
  
        var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]);   
        var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]);
        if (allStartDate.getTime() >= allEndDate.getTime()) { 
            return true;
        } else {
            return false;  
        }    
    } 

    /*
    日期格式化:
      对Date的扩展,将 Date 转化为指定格式的String
      年(y)可以用1-4个占位符,季度(q)可以用1-2个占位符.
      月(M)、日(d)、小时(h)、分(m)、秒(s)可以用1-2个占位符.
      毫秒(S)只能用1个占位符(是1-3位的数字) 
    例子: 
      (new Date()).Format("yyyy-MM-dd hh:mm:ss.S")
      (new Date()).Format("yyyy-MM-dd hh:mm:ss.S毫秒 第qq季度")
    */
    Date.prototype.Format = function (fmt) { 
        var o = {
            "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() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]):(("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

Date对象具有多种构造函数,下面简单列举如下:

  new Date() 
  new Date(milliseconds) 
  new Date(datestring) 
  new Date(year, month) 
  new Date(year, month, day) 
  new Date(year, month, day, hours) 
  new Date(year, month, day, hours, minutes) 
  new Date(year, month, day, hours, minutes, seconds) 
  new Date(year, month, day, hours, minutes, seconds, microseconds) 

下面对以上几个构造函数进行简单的分析:

  1.new Date(),没有参数的时候,创建的是当前时间日期对象。
  2.new Date(milliseconds),当参数为数字的时候表示毫秒数,创建一个距离1970年1月一日指定毫秒的时间日期对象。
  3.new Date(datestring),此参数是一个字符串,并且此字符串一定要能够使用Date.parse(datestring)转换。

    parse() 方法可解析一个日期时间字符串,并返回 1970年1月1日午夜距离该日期时间的毫秒数。
    语法:Date.parse(datestring)
    参数:datestring 是必需的,表示日期和时间的字符串。
    该方法是 Date 对象的静态方法。一般采用 Date.parse() 的形式来调用,而不是通过 dateobject.parse() 调用该方法。

  4.以下是其余六个构造函数的精确定义:
    1)year,是一个整数,如果是0-99,那么在此基础上加1900,其他的都原样返回。
    2)month,是一个整数,范围是0-11。
    3)day,是一个整数,范围是1-31。
    4)hours,是一个整数,范围是0-23。
    5)minutes,是一个整数,范围是0-59。
    6)seconds,是一个整数,范围是0-59。
    7)microseconds,是一个整数,范围是0-9999。

下面是js代码实例:

复制代码
  window.οnlοad=function(){
        var nowtime = new Date();//获取当前系统时间对象
        alert("nowtime:"+nowtime);
        var nowdate = nowtime.Format("yyyy-MM-dd hh:mm:ss");//格式化当前系统时间
        alert("nowdate:"+nowdate);

        var datestring = nowtime.getTime();//获取当前系统时间的时间戳
        alert("datestring:"+datestring);
        var mytime = new Date(datestring);//将时间戳转化为日期时间对象
        alert("mytime:"+mytime);
        
        var mydate = "2016-07-07 00:00:01";
        if(compareTime(mydate, nowdate)){//进行日期时间比较
            alert("指定时间没过期");
        }else{
            alert("指定时间已过期");
        }
    }

    //比较yyyy-MM-dd hh:mm:ss格式的日期时间大小
    function compareTime(startDate, endDate) {      
        var startDateTemp = startDate.split(" ");   
        var endDateTemp = endDate.split(" ");   
                       
        var arrStartDate = startDateTemp[0].split("-");   
        var arrEndDate = endDateTemp[0].split("-");   
      
        var arrStartTime = startDateTemp[1].split(":");   
        var arrEndTime = endDateTemp[1].split(":");   
  
        var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]);   
        var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]);
        if (allStartDate.getTime() >= allEndDate.getTime()) { 
            return true;
        } else {
            return false;  
        }    
    } 

    /*
    日期格式化:
      对Date的扩展,将 Date 转化为指定格式的String
      年(y)可以用1-4个占位符,季度(q)可以用1-2个占位符.
      月(M)、日(d)、小时(h)、分(m)、秒(s)可以用1-2个占位符.
      毫秒(S)只能用1个占位符(是1-3位的数字) 
    例子: 
      (new Date()).Format("yyyy-MM-dd hh:mm:ss.S")
      (new Date()).Format("yyyy-MM-dd hh:mm:ss.S毫秒 第qq季度")
    */
    Date.prototype.Format = function (fmt) { 
        var o = {
            "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() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]):(("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值