Rok raised the issue of having a date (really timestamp) literal syntax. Here is a modest proposal that fits into the current Date framework.

Motivation

ECMAScript 3 has literals (or at least initializers) for every type but timestamps. Having timestamp literals would probably help dissuade developers from the current practice of passing some (often) random string to Date.parse and ask it to figure out what it means. Since the input syntax to that function is not defined in the spec, this practice is is evil.

Proposal

Syntax

Timestamp literals are represented using a subset of ISO time syntax and semantics. A full timestamp literal has the following grammar (spaces are not allowed anywhere) with the exception that if the string looks like an identifier (eg “T12”) then it is an identifier:

If a timestamp represents an invalid time then a syntax error is thrown.

The value for an omitted field is taken from the current UTC time. If a timezone field is present, the current time converted to that timezone is used, otherwise UTC is used directly. (This may need tighter wording, and it may be the wrong thing. It’s possible that known default values should be used instead.)

The meaning of an integer f represented by a string of n digits after the period in the time part is the fractional second value f/10^n.

The method Date.parse shall be able to parse strings on this format, and it shall even be able to parse strings that look like identifiers (eg, T12).

A new method Date.prototype.toISOString shall generate strings on this format with all fields present and the shortest possible nonempty string of digits following the period in the time part.

Semantics

The evaluation of a date literal creates a new Date object with an internal time value corresponding to the time denoted by the date literal.

Prior art

A quick Google turns up date literals only in VBScript, which apparently allows #whatever# as a date literal, where “whatever” appears to be interpreted pretty liberally, ie, it doesn’t solve our problem