0

console.log(Date.parse('Wed Jul 07 04:49:10 IST 2021')); // returns NAN
console.log(Date.parse('Wed Jul 07 04:49:10 EDT 2021')); // working as expected 

How to convert IST dateString (Wed Jul 07 04:49:10 IST 2021) to number.

ruth
  • 29,535
  • 4
  • 30
  • 57
TamilRoja
  • 37
  • 6
  • Use [ISO 8601 format](https://xkcd.com/1179/) if you can, to ensure correct conversion. If you only get this format, you need to either parse it into ISO 8601 or use a library to do it for you. – VLAZ Jul 07 '21 at 09:27
  • I'm more surprised either works. Can you not amend the source format to be ISO8601, as this is much more standard and better supported. For example: `2021-07-07T04:49:10+0530` – Rory McCrossan Jul 07 '21 at 09:28
  • 1
    `Date.parse` expects _A string representing a simplification of the ISO 8601 calendar date extended format. (Other formats may be used, but results are implementation-dependent.)_ – Salman A Jul 07 '21 at 09:29
  • See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jul 07 '21 at 22:26
  • @SalmanA— *Date.parse* must also support the formats produced by *Date.prototype.toString* (specified in ECMA-262) and *Date.prototype.toUTCString* (specified in RFC 7231 and generalised in ECMA-262). ;-) – RobG Jul 07 '21 at 22:36

1 Answers1

2

As mentioned in comments, spec doesn't prescribe Date.parse() to handle the timezone abbreviations. Only a few of those are supported by major Date.parse() implementations out-of-the-box, and EDT is one of them. The biggest issue with recognizing IST is that this abbreviation might actually mean three different things:

... so it's quite difficult to parse the source string in a deterministic way. So your best bet would be replacing IST with a numeric timezone identifier (+05:30 for India Standard Time, for example).

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • @VLAZ— [*Date.parse*](https://262.ecma-international.org/#sec-date.parse) must also parse the format produced by [*Date.prototype.toString*](https://262.ecma-international.org/#sec-date.prototype.tostring) and [*date.prototype.toUTCString*](https://262.ecma-international.org/#sec-date.prototype.toutcstring), so there are 3 string formats it's required to parse (and everything else is implementation dependent). ;-) – RobG Jul 07 '21 at 22:30