Export (0) Print
Expand All

Formatting Date and Time Strings (JavaScript)

You can format date and time strings by using a simplified version of the ISO 8601 extended format. You can also use other formats for short dates, long dates, and times.

The following two lines of JavaScript syntax show statements that contain date and time strings.

JavaScript first attempts to parse a date string by using the ISO format. If the date string is not in correct ISO format, JavaScript attempts to parse the date by using other date parsing rules.

The ISO format is a simplification of the ISO 8601 extended format. The format is as follows:

YYYY-MM-DDTHH:mm:ss.sssZ

The following table describes the parts of this format.

Symbol

Description

Values

-, :, ., T

Characters actually in the string. T specifies the start of a time.

YYYY

Year. An extended year can be used instead of a 4-digit year. For more information, see Extended Years later in this topic.

MM

Month

01 to 12

DD

Day of the month

01 to 31

HH

Hours

00 to 24

mm

Minutes

00 to 59

ss

Seconds. The seconds and milliseconds are optional if a time is specified.

00 to 59

sss

Milliseconds

00 to 999

Z

The value in this position can be one of the following. If the value is omitted, UTC time is used.

  • Z indicates UTC time.

  • +hh:mm indicates that the input time is the specified offset after UTC time.

  • -hh:mm indicates that the input time is the absolute value of the specified offset before UTC time.

The string can include the date only, as in the following formats: YYYY, YYYY-MM, YYYY-MM-DD.

The ISO format does not support time zone names. You can use the Z position to specify an offset from UTC time. If you do not include a value in the Z position, UTC time is used.

You can specify midnight by using 00:00, or by using 24:00 on the previous day. The following two strings specify the same time: 2010-05-25T00:00 and 2010-05-24T24:00.

To return a date in ISO format, you can use the toISOString Method (JavaScript).

Extended Years

An extended year has 6 digits instead of 4 digits, and is prefixed with a plus or minus sign. An example of an extended year is +002010, which is equivalent to 2010. You can use an extended year to represent years before the year 0 or after 9999.

If you use the 6-digit format, a plus or minus sign must be present. When you use the 4-digit format, the sign must be absent. Therefore, 0000 and +000000 are accepted, but 000000 and -0001 cause an error. The extended year 0 is considered positive and therefore prefixed with a plus sign.

The toISOString Method (JavaScript) always uses the extended year format for years that are before 0 and after 9999.

NoteNote

ISO date formats are new in Internet Explorer 9 standards mode.

If a date string is not in the ISO format, JavaScript uses the following rules to parse it.

Short dates

  • The format must follow the month/day/year order, for example "06/08/2010".

  • Either "/" or "-" can be used as a separator.

Long dates

  • The year, month, and day can be in any order. "June 8 2010" and "2010 June 8" are both valid.

  • The year can have two or four digits. If the year has only two digits, it must be at least 70.

  • Month and day names must have at least two characters. Two character names that are not unique are resolved to the last matching name. For example, "Ju" specifies July, not June.

  • A day of the week is ignored if it is inconsistent with the rest of the supplied date. For example, "Tuesday November 9 1996" resolves to "Friday November 9 1996" because Friday is the correct day of the week for that date.

Times

  • Hours, minutes, and seconds are separated by colons. However, some of the parts can be omitted. The following are valid: "10:", "10:11", and "10:11:12".

  • If PM is specified and the specified hour is at least 13, NaN is returned. For example, "23:15 PM" returns NaN.

General

  • A string that contains an invalid date returns NaN. For example, a string that contains two years or two months returns NaN.

  • JavaScript supports all standard time zones, and Universal Coordinated Time (UTC) and Greenwich Mean Time (GMT). (The ISO format does not support time zones.)

  • Text enclosed in parentheses is treated as a comment. The parentheses can be nested.

  • Commas and spaces are treated as delimiters. Multiple delimiters are permitted.

The following code creates a table that contains the results of parsing several date and time strings. The resulting output table appears later in this section.

// Create a table that contains date parsing results.
document.write("<table border=1 cellpadding=4>");

// Add the header row.
AddHeaderRow();

// For each detail row, specify the date string to be parsed,
// and a comment about the date string.
AddRow("2010-06-09T15:20:00Z",
    "Specifies Z, which indicates UTC time.");
AddRow("2010-06-09T15:20:00-07:00",
    "Specifies -07:00 offset, which is equivalent to Pacific Daylight time.");
AddRow("2010-06-09T15:20:00",
    " Indicates UTC time because Z or offset is not specified.");
AddRow("2010-06-09T15:20:00.123Z",
    "Includes milliseconds.");

AddRow("2010", "Specifies year only.");
AddRow("2010-06", "Specifies year and month only.");
AddRow("2010-06-09", "Specifies date only.");

AddRow("June 9, 2010", "Specifies a non-ISO Long date.");
AddRow("2010 June 9", "Specifies a non-ISO Long date.");
AddRow("6/9/2010 3:20 pm", "Specifies a non-ISO Short date and time.");

document.write("</table>");


function AddRow(dateStr, comment) {
    // Parse the date.
    var dateResult = new Date(dateStr);

    // Add the row.
    document.write("<tr>");
    AddCell(dateStr);
    AddCell(dateResult.toUTCString());
    AddCell(dateResult.toISOString());
    AddCell(comment);
    document.write("</tr>");
}

function AddHeaderRow() {
    document.write("<tr>");
    AddCell("Input string");
    AddCell("toUTCString result");
    AddCell("toISOString result");
    AddCell("Comment about input");
    document.write("</tr>");
}

function AddCell(cellText) {
    document.write("<td>" + cellText + "</td>");
}

This code generates a table that resembles the following table. Where local times are specified, the result will vary depending on the time zone.

Input string

toUTCString result

toISOString result

Comment about input

2010-06-09T15:20:00Z

Wed, 9 Jun 2010 15:20:00 UTC

2010-06-09T15:20:00.000Z

Specifies Z, which indicates UTC time.

2010-06-09T15:20:00-07:00

Wed, 9 Jun 2010 22:20:00 UTC

2010-06-09T22:20:00.000Z

Specifies a -07:00 offset, which is equivalent to Pacific Daylight time.

2010-06-09T15:20:00

Wed, 9 Jun 2010 15:20:00 UTC

2010-06-09T15:20:00.000Z

Indicates UTC time because neither Z nor offset is specified.

2010-06-09T15:20:00.123Z

Wed, 9 Jun 2010 15:20:00 UTC

2010-06-09T15:20:00.123Z

Includes milliseconds.

2010

Fri, 1 Jan 2010 00:00:00 UTC

2010-01-01T00:00:00.000Z

Specifies year only.

2010-06

Tue, 1 Jun 2010 00:00:00 UTC

2010-06-01T00:00:00.000Z

Specifies year and month only.

2010-06-09

Wed, 9 Jun 2010 00:00:00 UTC

2010-06-09T00:00:00.000Z

Specifies date only.

June 9, 2010

Wed, 9 Jun 2010 07:00:00 UTC

2010-06-09T07:00:00.000Z

Specifies a non-ISO Long date.

2010 June 9

Wed, 9 Jun 2010 07:00:00 UTC

2010-06-09T07:00:00.000Z

Specifies a non-ISO Long date.

6/9/2010 3:20 pm

Wed, 9 Jun 2010 22:20:00 UTC

2010-06-09T22:20:00.000Z

Specifies a non-ISO Short date and time.

Community Additions

Show:
© 2014 Microsoft