js中日期函数的相关操作

来源:互联网 发布:linux 文件夹访问权限 编辑:程序博客网 时间:2024/06/04 23:34

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.onload=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.onload=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
原创粉丝点击