JavaScript中的日期时间函数

1、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)

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

(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() 调用该方法。

new Date(datestring) 和Date.parse(datestring) 使用的是同样的解析规律,只是一个返回 Date object 另一个返回毫秒数。官方规定它们的参数(日期字符串)需要符合 RFC2822 或者 ISO8601 的格式。举个例子,也就是对应的可以写成 "Mar 31 2015" 或者 "2015-03-31" 。

如果日期字符串不符合这两种标准,这两个函数对结果概不负责,不过就算符合标准了,结果还是有点不同的。

new Date("Mar 31 2015")    // Tue Mar 31 2015 00:00:00 GMT-0400 (EDT)
new Date("2015-03-31")     // Mon Mar 30 2015 20:00:00 GMT-0400 (EDT)

RFC2822 的格式如果不带时区,这两个函数会当做本地时区处理,而 ISO8601 格式的话这两个函数则会当做 UTC 时区处理。

为了防止这种情况,一种解决方案是每次格式化日期都严格指定时区,以防止各种幺蛾子情况出现,比如:

new Date("2015-03-31T00:00:00-04:00")    // Tue Mar 31 2015 00:00:00 GMT-0400 (EDT)

(4)以下是其余六个构造函数中参数的精确定义

   year,是一个整数,如果是0-99,那么在此基础上加1900,其他的都原样返回。

   month,是一个整数,范围是0-11。

   day,是一个整数,范围是1-31。

   hours,是一个整数,范围是0-23。

   minutes,是一个整数,范围是0-59。

   seconds,是一个整数,范围是0-59。

   microseconds,是一个整数,范围是0-9999。

3、Date对象的基本方法

var myDate = new Date();

myDate.getYear(); //获取当前年份(2位)

myDate.getFullYear(); //获取完整的年份(4位,1970-????)

myDate.getMonth(); //获取当前月份(0-11,0代表1月)

myDate.getDate(); //获取当前日(1-31)

myDate.getDay(); //获取当前星期X(0-6,0代表星期天)

myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)

myDate.getHours(); //获取当前小时数(0-23)

myDate.getMinutes(); //获取当前分钟数(0-59)

myDate.getSeconds(); //获取当前秒数(0-59)

myDate.getMilliseconds(); //获取当前毫秒数(0-999)

myDate.toLocaleDateString(); //获取当前日期

myDate.toLocaleTimeString(); //获取当前时间

myDate.toLocaleString( ); //获取日期与时间

4、日期格式化代码示例

以下代码实现了yyyy-MM-dd hh:mm:ss格式的日期字符串比较大小功能,其中的日期格式化方法可通用:

  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毫秒 第q季度")
    */
    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;
    }

 

posted @ 2016-08-03 11:47  一线大码  Views(467)  Comments(1Edit  收藏  举报