Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla
您的搜索结果

    Date.parse()

    这篇翻译不完整。请帮忙从英语翻译这篇文章

    概要

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

    语法

    Date.parse(dateString)

    参数

    dateString
    一个符合 RFC2822 或 ISO 8601 日期格式的字符串。

    描述

    parse 方法接受一个日期字符串(例如 "Dec 25, 1995"),并返回从1970-1-1 00:00:00 UTC到该日期字符串所表示日期的毫秒数。该方法在基于字符串值设置日期值时很有用,例如结合使用setTime() 方法和 Date 对象。

    parse 方法接受一个表示时间的字符串,返回相应的时间值。该方法可以接受符合 RFC2822 / IETF 日期语法 (RFC2822 Section 3.3) 的字符串,如 "Mon, 25 Dec 1995 13:30:00 GMT"。该方法能够理解美国大陆时区的缩写,但是为了更通用,应该使用时区偏移,如 "Mon, 25 Dec 1995 13:30:00 +0430" (格林威治的子午线向东偏移4小时30分钟)。如果没有指定时区,默认使用本地时区。GMT 和 UTC 被看作相等。 如果 RFC2822 Section 3.3 格式中不包含时区信息时,会以本地时区来解析日期字符串。

    ECMAScript 5 ISO-8601 日期格式支持

    另外,日期时间字符串也可以使用 ISO 8601 格式。例如,"2011-10-10" (仅日期)或 "2011-10-10T14:48:00" (日期和时间)能够作为参数被传递和解析。 The local time zone is used to interpret arguments in RFC2822 Section 3.3 format that do not contain time zone information.

    Note that while time zone specifiers are used during date string parsing to properly interpret the argument, they do not affect the value returned, which is always the number of milliseconds between January 1, 1970 00:00:00 UTC and the point in time represented by the argument.

    Because parse is a static method of Date, you always use it as Date.parse(), rather than as a method of a Date object you created.

    Differences in assumed time-zone

    The dateString of "March 7, 2014" returns a different date than "2014-03-07" unless the local time-zone is UTC. When converting a dateString of "March 7, 2014" the local time-zone is assumed. When converting a dateString of "2014-03-07" the UTC time-zone is assumed. This results in two different Date values depending on the format of the string that is being converted.

    例子

    例子:使用 Date.parse()

    如果 IPOdate 是一个日期对象,则可以把其设置为本地时间 1995年8月9日。如下:

    IPOdate.setTime(Date.parse("Aug 9, 1995"));
    

    其他例子:

    // Returns 807937200000 in time zone GMT-0300, and other values in other
    // timezones, since the argument does not specify a time zone.
    Date.parse("Aug 9, 1995");
    
    // Returns 807926400000 no matter the local time zone.
    Date.parse("Wed, 09 Aug 1995 00:00:00 GMT");
    
    // Returns 807937200000 in timezone GMT-0300, and other values in other
    // timezones, since there is no time zone specifier in the argument.
    Date.parse("Wed, 09 Aug 1995 00:00:00");
    
    // Returns 0 no matter the local time zone.
    Date.parse("Thu, 01 Jan 1970 00:00:00 GMT");
    
    // Returns 14400000 in timezone GMT-0400, and other values in other 
    // timezones, since there is no time zone specifier in the argument.
    Date.parse("Thu, 01 Jan 1970 00:00:00");
    
    // Returns 14400000 no matter the local time zone.
    Date.parse("Thu, 01 Jan 1970 00:00:00 GMT-0400");
    

    规范

    规范版本 规范状态 注解
    ECMAScript 1st Edition. Implemented in JavaScript 1.0 Standard Initial definition.
    ECMAScript 5.1 (ECMA-262)
    Date.parse
    Standard ISO 8601 format added
    ECMAScript 6 (ECMA-262)
    Date.parse
    Draft  

    浏览器兼容性

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
    ISO 8601 format (Yes) 4.0 (2.0) 9 (Yes) (Yes)
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support ? (Yes) (Yes) ? ? ?
    ISO 8601 format ? (Yes) (Yes) ? ? ?

    相关链接

    文档标签和贡献者

    标签: 
    此页面的贡献者有: fscholz, AlexChao
    最后编辑者: fscholz,