Javascrpt之Date

Date的构造器

作为函数调用 Date 构造器

  • 当把 Date 作为函数来调用,而不作为构造器,它返回一个表示当前时间(协调世界时)的字符串,即不是用 new 关键字。

Date 构造器

  1. new Date();
  2. new Date(value);
  3. new Date(dateString);
  4. new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);

value

  • 代表自1970年1月1日00:00:00 (时间标准时间) 起经过的毫秒数。

dateString

  • 表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别(符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601)。

year

  • 代表年份的整数值。为了避免2000年问题最好指定4位数的年份; 使用 1998, 而不要用 98.

month

  • 代表月份的整数值从0(1月)到11(12月)。

day

  • 代表一个月中的第几天的整数值,从1开始。

hour

  • 代表一天中的小时数的整数值 (24小时制)。

minute

  • 分钟数。

second

  • 秒数。

millisecond

  • 表示时间的毫秒部分的整数值。
var today = new Date();
var today = new Date(1453094034000);// by timestamp(accurate to the millimeter)
var birthday = new Date("December 17, 1995 03:24:00");
var birthday = new Date("1995-12-17T03:24:00");
var birthday = new Date(1995,11,17);
var birthday = new Date(1995,11,17,3,24,0);

Date 构造器的属性

Date.prototype

  • Date的初始值是数组原型对象。

Date.parse (string)

  • 解析一个表示日期对象的字符串,并返回从1970-1-1 00:00:00 UTC 到该日期对象(该日期对象的UTC时间)的毫秒数。

    Date.parse("March 3, 2016"); // 1456934400000
    

Date.UTC (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ])

  • 接受的参数同日期构造函数接受最多参数时一样,返回从1970-1-1 00:00:00 UTC到指定日期的的毫秒数。

year

  • 1900 年后的某一年份。

month

  • 0 到 11 之间的一个整数,表示月份。

date

  • 1 到 31 之间的一个整数,表示某月当中的第几天。

hours

  • 0 到 23 之间的一个整数,表示小时。

minutes

  • 0 到 59 之间的一个整数,表示分钟。

seconds

  • 0 到 59 之间的一个整数,表示秒。

ms

  • 0 到 999 之间的一个整数,表示微秒。

    var utcDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0)); // Sun Dec 01 1996 08:00:00 GMT+0800 (中国标准时间)
    

Date.now()

  • 返回自1970年1月1日 00:00:00 UTC到当前时间的毫秒数。

    var timeInMs = Date.now(); // 1456994557379
    

Date 原型对象的方法

Date.prototype.constructor

  • Date.prototype.constructor 的初始值是内置 Date 构造器。

Date.prototype.toString ( )

  • 返回一个字符串,表示该Date对象。

    var x = new Date();
    myVar = x.toString(); // "Thu Mar 03 2016 16:46:10 GMT+0800 (中国标准时间)"
    

Date.prototype.toDateString ( )

  • 以美式英语和人类易读的形式返回一个日期对象日期部分的字符串。

    var x = new Date();
    myVar = x.toDateString(); // "Thu Mar 03 2016"
    

Date.prototype.toTimeString ( )

  • 以人类易读形式返回一个日期对象时间部分的字符串,该字符串以美式英语格式化。

    var x = new Date();
    myVar = x.toString(); // "16:46:10 GMT+0800 (中国标准时间)"
    

Date.prototype.toLocaleString ( )

  • 返回该日期对象的字符串,该字符串格式因不同语言而不同。新增的参数 locales 和 options 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。在旧版本浏览器中, locales 和 options 参数被忽略,使用的语言环境和返回的字符串格式是各自独立实现的。
  • 详细查看火狐javascript标准库,兼容效果不好的方法。

Date.prototype.toLocaleDateString ( )

  • 同上

Date.prototype.toLocaleTimeString ( )

  • 同上

Date.prototype.valueOf ( )

  • 返回一个日期对象的原始值。和 Date.prototype.getTime() 效果一致

    var x = new Date(2016, 6, 17);
    var myVar = x.valueOf();
    console.log(myVar); // 1468684800000
    

Date.prototype.getTime ( )

  • 返回一个时间的格林威治时间数值。

    var x = new Date(2016, 6, 17);
    var myVar = x.getTime();
    console.log(myVar); // 1468684800000
    

Date.prototype.getFullYear ( )

  • 根据本地时间,返回一个指定日期对象的年份。

    var today = new Date();
    var yr = today.getFullYear();
    console.log(yr); // 2016
    

Date.prototype.getUTCFullYear ( )

  • 以世界时为标准,返回一个指定的日期对象的年份。

    var today = new Date();
    var yr = today.getUTCFullYear();
    console.log(yr); // 2016
    

Date.prototype.getMonth ( )

  • 根据本地时间,返回一个指定的日期对象的月份,为基于0的值(0表示一年中的第一月)。

    var today = new Date(); // 2016-3-3
    var m = today.getMonth();
    console.log(m); // 2
    

Date.prototype.getUTCMonth ( )

  • 以世界时为标准,返回一个指定的日期对象的月份,它是从 0 开始计数的(0 代表一年的第一个月)。

    var today = new Date(); // 2016-3-3
    var m = today.getUTCMonth();
    console.log(m); // 2
    

Date.prototype.getDate ( )

  • 根据本地时间,返回一个指定的日期对象为一个月中的第几天。

    var today = new Date(); // 2016-3-3
    var d = today.getDate();
    console.log(d); // 3
    

Date.prototype.getUTCDate ( )

  • 以世界时为标准,返回一个指定的日期对象为一个月中的第几天。

    var today = new Date(); // 2016-3-3
    var d = today.getUTCDate();
    console.log(d); // 3
    

Date.prototype.getDay ( )

  • 根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天。

    var today = new Date(); // 2016-3-3
    var d = today.getDay();
    console.log(d); // 4
    

Date.prototype.getUTCDay ( )

  • 以世界时为标准,返回一个具体日期中一周的第几天,0 表示星期天。

    var today = new Date(); // 2016-3-3
    var d = today.getUTCDay();
    console.log(d); // 4
    

Date.prototype.getHours ( )

  • 根据本地时间,返回一个指定的日期对象的小时。

    var today = new Date(); // 2016-3-3 17:09
    var h = today.getHours();
    console.log(h); // 17
    

Date.prototype.getUTCHours ( )

  • 以世界时为标准,返回一个指定的日期对象的小时。

    var today = new Date(); // 2016-3-3 17:09
    var h = today.getUTCHours();
    console.log(h); // 9
    

Date.prototype.getMinutes ( )

  • 根据本地时间,返回一个指定的日期对象的分钟数。

    var today = new Date(); // 2016-3-3 17:09
    var m = today.getMinutes();
    console.log(m); // 9
    

Date.prototype.getUTCMinutes ( )

  • 以世界时为标准,返回一个指定的日期对象的分钟数。

    var today = new Date(); // 2016-3-3 17:09
    var m = today.getUTCMinutes();
    console.log(m); // 9
    

Date.prototype.getSeconds ( )

  • 根据本地时间,返回一个指定的日期对象的秒数。

    var today = new Date(); // 2016-3-3 17:09:09
    var m = today.getSeconds();
    console.log(m); // 9
    

Date.prototype.getUTCSeconds ( )

  • 以世界时为标准,返回一个指定的日期对象的秒数。

    var today = new Date(); // 2016-3-3 17:09:09
    var m = today.getUTCSeconds();
    console.log(m); // 9
    

Date.prototype.getMilliseconds ( )

  • 根据本地时间,返回一个指定的日期对象的毫秒数。

    var today = new Date(); // 2016-3-3 17:09:09
    var m = today.getMilliseconds();
    console.log(m); // 662
    

Date.prototype.getUTCMilliseconds ( )

  • 以世界时为标准,返回一个指定的日期对象的毫秒数。

    var today = new Date(); // 2016-3-3 17:09:09
    var m = today.getUTCMilliseconds();
    console.log(m); // 662
    

Date.prototype.getTimezoneOffset ( )

  • 返回协调世界时(UTC)相对于当前时区的时间差值,单位为分钟。

    var x = new Date();
    var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
    console.log(currentTimeZoneOffsetInHours); // -8
    

Date.prototype.setTime (time)

  • 以一个表示从1970-1-1 00:00:00 UTC计时的毫秒数为来为 Date 对象设置时间。

    theBigDay = new Date("July 1, 1999");
    sameAsBigDay = new Date();
    sameAsBigDay.setTime(theBigDay.getTime());
    console.log(sameAsBigDay); // 930758400000
    

Date.prototype.setMilliseconds (ms)

  • 根据本地时间设置一个日期对象的微秒数。0-999

    var theBigDay = new Date();
    theBigDay.setMilliseconds(100); // 1456996902100
    

Date.prototype.setUTCMilliseconds (ms)

  • 根据世界时间(UTC)设置一个日期对象的微秒数。0-999

    var theBigDay = new Date();
    theBigDay.setUTCMilliseconds(100); // 1456996902100
    

Date.prototype.setSeconds (sec [, ms ] )

  • 根据本地时间设置一个日期对象的秒数。0-59

    var theBigDay = new Date();
    theBigDay.setSeconds(10); // 1456996910100
    

Date.prototype.setUTCSeconds (sec [, ms ] )

  • 根据世界时间(UTC)设置一个日期对象的秒数。0-59

    var theBigDay = new Date();
    theBigDay.setUTCSeconds(10); // 1456996910100
    

Date.prototype.setMinutes (min [, sec [, ms ] ] )

  • 根据本地时间设置一个日期对象的分钟数。0-59

    var theBigDay = new Date();
    theBigDay.setMinutes(10); // 1456996233412
    

Date.prototype.setUTCMinutes (min [, sec [, ms ] ] )

  • 根据世界时间(UTC)设置一个日期对象的分钟数。0-59

    var theBigDay = new Date();
    theBigDay.setUTCMinutes(10); // 1456996233412
    

Date.prototype.setHours (hour [, min [, sec [, ms ] ] ] )

  • 根据本地时间为一个日期对象设置小时数,返回从1970-01-01 00:00:00 UTC 到更新后的 日期 对象实例所表示时间的毫秒数。

    var theBigDay = new Date();
    theBigDay.setHours(10); // 1457001538652
    

Date.prototype.setUTCHours (hour [, min [, sec [, ms ] ] ] )

  • 根据世界时间(UTC)为一个日期对象设置小时数,返回从1970-01-01 00:00:00 UTC 到更新后的 日期 对象实例所表示时间的毫秒数。

    var theBigDay = new Date();
    theBigDay.setUTCHours(10); // 1457001538652
    

Date.prototype.setDate (date)

  • 根据本地时间来指定一个日期对象的天数。

    var theBigDay = new Date();
    theBigDay.setDate(10); // 1457001538652
    

Date.prototype.setUTCDate (date)

  • 根据世界时间(UTC)来指定一个日期对象的天数。

    var theBigDay = new Date();
    theBigDay.setUTCDate(10); // 1457001538652
    

Date.prototype.setMonth (month [, date ] )

  • 根据本地时间来指定一个日期对象的月份。

    var theBigDay = new Date();
    theBigDay.setMonth(10); // 1457001538652
    

Date.prototype.setUTCMonth (month [, date ] )

  • 根据世界时间(UTC)来指定一个日期对象的月份。

    var theBigDay = new Date();
    theBigDay.setUTCMonth(10); // 1457001538652
    

Date.prototype.setFullYear (year [, month [, date ] ] )

  • 根据本地时间来指定一个日期对象的年份。

    var theBigDay = new Date();
    theBigDay.setFullYear(2016); // 1425375645914
    

Date.prototype.setUTCFullYear (year [, month [, date ] ] )

  • 根据世界时间(UTC)来指定一个日期对象的年份。

    var theBigDay = new Date();
    theBigDay.setUTCFullYear(2016); // 1425375645914
    

Date.prototype.toUTCString ( )

  • 把一个日期转换为一个字符串,使用UTC时区。

    var today = new Date();
    var UTCstring = today.toUTCString(); // Mon, 03 Jul 2006 21:44:38 GMT
    

Date.prototype.toISOString ( )

  • 返回一个 ISO(ISO 8601 Extended Format)格式的字符串: YYYY-MM-DDTHH:mm:ss.sssZ。时区总是UTC(协调世界时),加一个后缀“Z”标识。

    var today = new Date("05 October 2011 14:48 UTC");
    alert(today.toISOString()); // 返回2011-10-05T14:48:00.000Z
    

Date.prototype.toJSON ( key )

  • 方法返回日期对象的字符串表示形式。

    var jsonDate = (new Date()).toJSON(); // "2016-03-03T09:35:41.783Z"
    var backToDate = new Date(jsonDate); // "Thu Mar 03 2016 17:35:41 GMT+0800 (中国标准时间)"
    

参考火狐标准库

posted @ 2016-03-08 15:18  小鱼Gus  阅读(1023)  评论(0编辑  收藏  举报