20.3.4.35 Date.prototype.toDateString ( )
20.3.4.42 Date.prototype.toTimeString ( )
20.3.4.41.1 Runtime Semantics: ToDateString(tv) returns "Invalid Date" for NaN values, but 20.3.4.35 and 20.3.4.42 don't handle this case.
I think this is one we can let go until ES7.
TC39 explicitly decided to specify "Invalid Date" for toString. But I don't recall whether we also explicitly talked about toDateString and toTimeString. I suspect we didn't and that is the reason they weren't updated.
Because their result is implementation defined, an implementation can do this if it wants to.
Have you test the behavior on the major browsers?
new Date(NaN).toDateString() and new Date(NaN).toTimeString() return "Invalid Date" in:
- IE11, Win7-de-DE
- Edge, Win10-en-US-build10074
- V8 (see below for source info)
- JavaScriptCode (see below for source info)
- Nashorn (see below for source info)
- SpiderMonkey (see below for source info)
I've also tested 20.3.4.43 Date.prototype.toUTCString ( ):
new Date(NaN).toUTCString() returns "Invalid Date" in all tested engines, except for Nashorn. Nashorn throws a RangeError.
V8
toDateString: https://github.com/v8/v8-git-mirror/blob/cf420ec337e47bed2c754d4ebdc87e8d1bb3cfe5/src/date.js#L304
toTimeString: https://github.com/v8/v8-git-mirror/blob/cf420ec337e47bed2c754d4ebdc87e8d1bb3cfe5/src/date.js#L312
toUTCString: https://github.com/v8/v8-git-mirror/blob/cf420ec337e47bed2c754d4ebdc87e8d1bb3cfe5/src/date.js#L634
Constant "kInvalidDate": https://github.com/v8/v8-git-mirror/blob/cf420ec337e47bed2c754d4ebdc87e8d1bb3cfe5/src/macros.py#L57
JavaScriptCode
toDateString: https://github.com/WebKit/webkit/blob/0f0b75cd1fe1c64499b65e64b4ee15d015b733f4/Source/JavaScriptCore/runtime/DatePrototype.cpp#L549
toTimeString: https://github.com/WebKit/webkit/blob/0f0b75cd1fe1c64499b65e64b4ee15d015b733f4/Source/JavaScriptCore/runtime/DatePrototype.cpp#L555
toUTCString: https://github.com/WebKit/webkit/blob/0f0b75cd1fe1c64499b65e64b4ee15d015b733f4/Source/JavaScriptCore/runtime/DatePrototype.cpp#L509
Format function "formateDateInstance": https://github.com/WebKit/webkit/blob/0f0b75cd1fe1c64499b65e64b4ee15d015b733f4/Source/JavaScriptCore/runtime/DatePrototype.cpp#L332
Nashorn
toDateString: http://hg.openjdk.java.net/jdk9/dev/nashorn/file/b275aac76cdd/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeDate.java#l281
toTimeString: http://hg.openjdk.java.net/jdk9/dev/nashorn/file/b275aac76cdd/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeDate.java#l292
toUTCString: http://hg.openjdk.java.net/jdk9/dev/nashorn/file/b275aac76cdd/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeDate.java#l819
http://hg.openjdk.java.net/jdk9/dev/nashorn/file/b275aac76cdd/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeDate.java#l1015
Format function "toStringImpl": http://hg.openjdk.java.net/jdk9/dev/nashorn/file/b275aac76cdd/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeDate.java#l988
Constant "INVALID_DATE": http://hg.openjdk.java.net/jdk9/dev/nashorn/file/b275aac76cdd/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeDate.java#l59
SpiderMonkey
toDateString: http://hg.mozilla.org/mozilla-central/file/4b9b12c248dc/js/src/jsdate.cpp#l2824
toTimeString: http://hg.mozilla.org/mozilla-central/file/4b9b12c248dc/js/src/jsdate.cpp#l2811
toUTCString: http://hg.mozilla.org/mozilla-central/file/4b9b12c248dc/js/src/jsdate.cpp#l2402
Format function "format_date": http://hg.mozilla.org/mozilla-central/file/4b9b12c248dc/js/src/jsdate.cpp#l2534
Constant "js_NAN_date_str": http://hg.mozilla.org/mozilla-central/file/4b9b12c248dc/js/src/jsdate.cpp#l2345
defer to ES7