What's the date time string format in JavaScript?

Date time string can be used as the argument of Date.parse method or the Date constructor in JavaScript.

According to the specification of Date Time String Format:

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ.

The table of all fields’ meanings:

YYYY MM DD HH mm ss sss Z - T : .
Year Month Day Hour Minute Second Millisecond TimeZoneOffset Literally
0000-9999 01-12 01-31 00-24 00-59 00-59 000-999 Z / ±HH:mm -

Note: If the time is in UTC, just use “Z” directly for Z. “Z” is the zone designator for the zero UTC offset. Offset from UTC are write in the format “±HH:mm”. For example, China’s Z should be +08:00.

There is a picture contains all UTC time zone offsets. 本当に面白い!

GMT

GMT means the Greenwich Mean Time, formerly used as the international civil time standard, which is superseded in that function by Coordinated Universal Time, because of earth’s uneven speed in its elliptical orbit and its axial tilt.

UT

UT, Universal Time, is a time standard based on Earth’s rotation. It’s a modern continuation of GMT. There are several versions of it, like UTC, UT1… All UT except for UTC are based on Earth’s rotation relative to distant celestial objects, but with a scaling factor and other adjustments to make them closer to solar time. UTC is based on International Atomic Time, with leap seconds added to keep it within 0.9 second of UT1.

UTC

UTC, Coordinated Universal Time, is the primary time standard by which the world regulates clock and time. It is closely related to GMT. For most purposes, UTC is considered interchangeable with GMT, but GMT is no longer precisely defined by the scientific community.

ISO 8601

ISO 8601 is an international standard covering the exchange of date and time-related data, issued by ISO. The purpose of this is to provide an unambiguous and well-defined method of representing dates and times.

(It shocked me that the standard should charge…=>link)

Date String in Action

There are the results of all Date‘s toString methods in JavaScript: (I’m in China now.)

Method Result
toString Mon Jul 04 2016 22:54:38 GMT+0800 (中国标准时间)
toJSON 2016-07-04T14:54:38.766Z
toDateString Mon Jul 04 2016
toTimeString 22:54:38 GMT+0800 (中国标准时间)
toGMTString Mon, 04 Jul 2016 14:54:38 GMT
toUTCString Mon, 04 Jul 2016 14:54:38 GMT
toISOString 2016-07-04T14:54:38.766Z
toLocaleString 2016/7/4 下午10:54:38
toLocaleDateString 2016/7/4
toLocaleTimeString 下午10:54:38

Now I know that GMT and UTC are time standards while ISO 8601 provides the representation form of time.

A Library

Moment.js, a library that parses, validates, manipulates, and displays dates in JavaScript. This website is using this library now.

Example:

1
2
moment('20160704', 'YYYYMMDD').format('MMMM D, YYYY');
// July 4, 2016, exactly the title of this website.
Comments