53483

Why does parsing a locale date string result in an invalid date?

Question:

Can someone explain why the following snippets result in an invalid date object?

new Date(new Date().toLocaleString()) // or Date.parse(new Date().toLocaleString())

Answer1:

This is expressly permitted by the <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.4.2" rel="nofollow">ES5 specification's definition of Date.parse</a> (emphasis mine):

<blockquote>

...all of the following expressions should produce the same numeric value in that implementation, if all the properties referenced have their initial values:

x.valueOf() Date.parse(x.toString()) Date.parse(x.toUTCString()) Date.parse(x.toISOString())

However, the expression

Date.parse(x.toLocaleString())

<strong>is not required to produce the same Number value</strong> as the preceding three expressions and, in general, <strong>the value produced by Date.parse is implementation-dependent when given any String value that does not conform to the Date Time String Format</strong> (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15" rel="nofollow">15.9.1.15</a>) and that could not be produced in that implementation by the toString or toUTCString method.

</blockquote>

Since toLocaleString is not required to produce a string conformant to the Date Time String Format YYYY-MM-DDTHH:mm:ss.sssZ, it is allowable for its output not to be parsed correctly by Date.parse.

Answer2:

<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString" rel="nofollow">new Date().toLocaleString()</a> returns the current date in a format <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Syntax" rel="nofollow">new Date()</a> can't parse, resulting in unexpected dates.

Recommend

  • why '\\97' ascii value equals 55
  • Java: Issue with available() method of BufferedInputStream
  • Is static member variable initialized in a template class if the static menber is not used?
  • Capture video without preview
  • C++ calling the default constructor with parens vs without parens [duplicate]
  • How to negative match regex in JavaScript string replace? [duplicate]
  • Plot a table with R
  • How to solve “The hierarchy of the type is inconsistent” error in Java Eclipse?
  • Javascript - Waiting for event before proceeding
  • What are zone turns?
  • Get XML response value with GDataXML
  • How do I know what reference to include to import a specific .NET namespace?
  • Edit assembly language code in Visual Studio while stepping through each statement
  • addressing in assembler
  • Jhipster: How to create relationships with User entity using supplied tools?
  • Google Player Game Service Error: Can't build the sample game
  • How do I compile a C/C++ program through windows command prompt?
  • SQL Server re-calculate or not?
  • Neo4j: Filter nodes based on aggregate function
  • FTS3 searches in ORMLite?
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • Use sed with regex and (
  • What's the syntax to inherit documentation from another indexer?
  • abstracting over a collection
  • there is no graph with tensorboard
  • Multiple producers single consumer locking schema
  • Can't remove headers after they are sent
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • How to revert to previous XCode version?
  • Access Android Market through SSH tunnel
  • Not able to aggregate on nested fields in elasticsearch
  • gspread or such: help me get cell coordinates (not value)
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • ImageMagick, replace semi-transparent white with opaque white
  • Q promise. Difference between .when and .then
  • python regex in pyparsing
  • Error creating VM instance in Google Compute Engine
  • C# - Getting references of reference