Export (0) Print
Expand All
Expand Minimize

2.1.38 [ECMA-262/5] Section 15.9.4.2, Date.parse (string)

V0038:

The specification states:

The parse function applies the ToString operator to its argument and interprets the resulting String as a date and time; it returns a Number, the UTC time value corresponding to the date and time. The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

IE9 Mode, IE10 Mode, and IE11 Mode (All Versions)

If the argument string for the parse function does not conform to the Date Time String Format, the parse function tries to parse the string value and it produces a value in accordance with the following grammar and rules. If the string cannot be recognized starting with the DateString production, the NaN number value is returned.

Date String Syntax

The following lexical grammar defines the tokens that make up date strings.

DateToken ::

      Separator

      NumericDateToken

      AlphaDateToken

      DateComment

      OffsetFlag

Separator :: one of

      , : / <SP>

DateComment ::

       ( DateCommentBodyopt )

DateCommentBody ::

      DateCommentChars DateCommentopt

      DateComment DateCommentBodyopt

DateCommentChars ::

      DateCommentChar DateCommentCharsopt

DateCommentChar ::

      DateChar but not ( or )

OffsetFlag :: one of

      + -

AlphaDateToken ::

      AlphaDateComponent periodopt

AlphaDateComponent ::

      WeekDay

      Month

      TimeZone

      MilitaryTimeZone

      AmPmFlag

      AdBcFlag

period ::

      .

WeekDay ::

      Sunday

      Monday

      Tuesday

      Wednesday

      Thursday

      Friday

      Saturday

Month ::

      January

      February

      March

      April

      May

      June

      July

      August

      September

      October

      November

      December

TimeZone ::

      est

      edt

      cst

      cdt

      mst

      mdt

      pst

      pdt

      gmt

      utc

MilitaryTimeZone ::

      a [lookahead {.m m d .d p u}]

      p [lookahead {.m m d s}]

      b [lookahead {.c c}]

      f [lookahead {e i}]

      m [lookahead {a d o s}]

      s [lookahead {a e u}]

      o [lookahead ≠ c]

      n [lookahead ≠ o]

      d [lookahead ≠ e]

      t [lookahead {h u}]

      w [lookahead ≠ e]

      e [lookahead {d s}]

      c [lookahead {d s}]

      g [lookahead ≠ m]

      u [lookahead ≠ t

      UniqueMilitaryTimeZone

UniqueMilitaryTimeZone :: one of

      z y x v r q h i k l

AmPmFlag ::

      am

      a.m

      pm

      p.m

AdBcFlag ::

      ad

      a.d

      bc

      b.c

NumericDateToken ::

      NumericDateComponent -

      NumericDateComponent [lookahead ≠ -]

NumericDateComponent ::

      DateDigit [lookahead DateDigit]

      DateDigit DateDigit [lookahead DateDigit]

      DateDigit DateDigit DateDigit [lookahead DateDigit]

      DateDigit DateDigit DateDigit DateDigit [lookahead DateDigit]

      DateDigit DateDigit DateDigit DateDigit DateDigit [lookahead DateDigit]

      DateDigit DateDigit DateDigit DateDigit DateDigit DateDigit [lookahead DateDigit]

DateDigit :: one of

      0 1 2 3 4 5 6 7 8 9

Sunday ::

      su

      sun

      sund

      sunda

      sunday

Monday ::

      mo

      mon

      mond

      monda

      monday

Tuesday ::

      tu

      tue

      tues

      tuesd

      tuesda

      tuesday

Wednesday ::

      we

      wed

      wedn

      wedne

      wednes

      wednesd

      wednesda

      wednesday

Thursday ::

      th

      thu

      thur

      thurs

      thursd

      thursda

      thursday

Friday ::

      fr

      fri

      frid

      frida

      friday

Saturday ::

      sa

      sat

      satu

      satur

      saturd

      saturda

      saturday

January ::

      ja

      jan

      janu

      januar

      january

February ::

      fe

      feb

      febr

      febru

      februa

      februar

      february

March ::

      ma

      mar

      marc

      march

April ::

      ap

      apr

      apri

      april

May ::

      ma

      may

June ::

      jun

      june

July ::

      ju

      jul

      july

August ::

      au

      aug

      augu

      augus

      august

September ::

      se

      sep

      sept

      septe

      septem

      septemb

      septembe

      september

October ::

      oc

      oct

      octo

      octob

      octobe

      october

November ::

      no

      nov

      nove

      novem

      novemb

      novembe

      november

December ::

      de

      dec

      dece

      decem

      decemb

      decembe

      december

Parsing Rules for Date.parse Date Strings

  1. The string to be parsed is converted to lowercase and then these rules are applied.

  2. The preceding grammar syntax uses NumericDateToken literals or AlphaDateToken literals to define the following components of a date object: weekday, year, month, date, hours, minutes, seconds, time zone, AD/BC flag, and AM/PM flag.

  3. Any date string must define at least year, month, and date components. No component can be defined multiple times.

  4. Components can be in any order, except for cases that are explicitly specified otherwise.  

  5. The following rules apply to the OffsetFlags literal:

    • The plus sign (+) and minus sign (-) are offset classifiers, when they do not follow a number. The next numeric component that follows an offset classifier is classified as an offset value. The numeric component does not have to follow immediately after the plus sign (+) or minus sign (-).

    • The + offset and the – offset cannot be specified before the year field. + or - offsets refer to the UTC time zone and set the time zone to UTC. A time zone component cannot follow a + or - offset.

  6. The colon (:) separator char acts as a time classifier for numeric components:

    • A colon (:) that follows a number classifies the previous numeric component as hours.

    • A colon (:) that follows a number that is classified as an hour classifies the next numeric component as minutes. The next numeric component does not have to immediately follow the colon.

    • A colon (:) that follows a number that is classified as a minute classifies the next numeric component as seconds. The next number does not have to immediately follow the colon.

  7. The following rules define date classification for numeric components:

    • A number that is not classified and that has a value that is greater than or equal to 70 is always classified as years. Even when such a number is followed by a colon (:) and could be classified as hours, the number is classified as years. In this case, the colon (:) is a simple separator.

    • A number that is not classified by a classifier is always classified as a date.

    • Forward slash (/) and hyphen (-) separator chars can act as classifiers in the following ways:

    • A forward slash (/) or hyphen (-) that follows a numeric component classifies that numeric component as months.

    • A forward slash (/) or hyphen (-) that follows a numeric component that is classified as a month classifies the next numeric component as a date. The next numeric component does not have to immediately follow the forward slash or the hyphen.

    • A forward slash (/) or hyphen (-) that follows a numeric component that is classified as a date classifies the next numeric component as a year. The next numeric component does not have to immediately follow the forward slash or the hyphen.

  8. The week day is ignored regardless of whether it is correct or incorrect.

  9. The default value for the AD/BC flag is AD.

  10. When the AM/PM flag is not defined, the default interpretation for hours is 24-hour notation. The AM flag is ignored when the time is greater than 13:00:00. When the PM flag is used, the time must be less than 12:00.

Algorithm for Computing the Time Value

Numeric values are calculated for year, month, date, and time through classification, numeric components, and alpha components. The following adjustments are done because of the flags, offsets, and time zones:

  1. If the BC/AD flag is BC, year = -year + 1.

    Note 1 BC is year 0 and 2 BC is year -1.

  2. If the BC/AD flag is AD and the year value is less than 100, year = year+1900. This rule allows the short form for the year value. For example, 99 stands for 1999.

  3. The time value (that is, the time during the day) is calculated in seconds from the hour, minute, and seconds components. The AM/PM flag can change the time value as follows:

    • If no AM/PM flag is present, the time is considered to be in 24-hour notation and no adjustment is done.

    • If the time is greater than or equal to 12 * 3600 and the time is less than 13*3600 and if the AM/PM flag is AM, time = time – 12*3600. For example, 12:45 AM means 0:45.

    • If the AM/PM flag is PM and the time is less than 12*3600, time = time + 12 *3600. For example, 2PM means 14:00.

  4. Time zone adjustment. The time value (from rule 3) is adjusted by the zone display values that are specified in the following tables. Check the TimeZone and MilitaryTimeZone values. If zone is the value for a given zone, the time is adjusted by: time = time - zone * 60.

  5. Offset adjustment. The offset value applies to the time in the UTC zone. Let nn be the value of the numeric component that follows an offset. The following formulas define the offset value, in seconds, that then add up to the UTC time:

    • If nn <24: vOffset = 60* nn * 60

    • If nn >=24: vOffset = 60* (nn modulo 100) + (floor (nn / 100)) * 60))

    • time = Result(4) - vOffset * 60;

  6. Date adjustment. Set date = date -1.

  7. Month adjustment. Set month = (month-1).

  8. Final time calculation:

    • year = year + floor(month / 12);

    • month = Remainder(month, 12)

    • day = day + DayFromYear(year);

    • day = day + DayNumbersForTheMonthOfALeapYear(month);

    • If month is greater than or equal to 2 and the year is not a leap year, day = day - 1;

    • result = day * 86400000 + time;

  9. If no time zone is specified, consider the time to be in the current local time zone and then get the UTC displacement of the time.

TimeZone value

UTC displacement

est

-5

edt

-4

cst

-6

cdt

-5

mst

-7

mdt

-6

pst

-8

pdt

-7

gmt

0

utc

0

MilitaryTimeZone value

UTC displacement

z

0

y

12

x

11

w

10

v

9

u

8

t

7

s

6

r

5

q

4

p

3

o

2

n

1

a

-1

b

-2

c

-3

d

-4

e

-5

f

-6

g

-7

h

-8

i

-9

k

-10

l

-10

m

12

Show:
© 2014 Microsoft