0

I'm attempting to learn JS, and I'm running into an issue with parsing a Date into a usable format.

I have a date in this format.

Sat Apr 09 2016 19:23:29 GMT-0500 (Central Daylight Time)

I've tried using SimpleDateFormat parse, like this:

SimpleDateFormat sdf = new SimpleDateFormat("E M d yyyy HHmmSS z");  
Date date = sdf.parse("Sat Apr 09 2016 19:23:29 GMT-0500 (Central Daylight Time)");

What would be the best way to parse this? Or do I need to create a function that will go through each "index" of the string and parse it manually?

  • 2
    Um, that is not valid javascript code. Are you confusing javascript with java? – Jared Smith Apr 11 '16 at 00:26
  • I might have found the wrong code. I have spent the last 10-15 minutes trying to find a javascript answer and I suppose I mistakenly found java. But I am looking for javascript. – CapitalLlama Apr 11 '16 at 00:28
  • Try `new Date(myDateString);` – Jared Smith Apr 11 '16 at 00:30
  • @JaredSmith—no, don't do that. – RobG Apr 11 '16 at 00:33
  • @RobG that looks like the output of `Date.toString()` which in every browser I know of is a reversible transformation (despite the fact the constructor technically takes integers). Or were you chiding me for commenting on a dupe? – Jared Smith Apr 11 '16 at 00:39
  • @JaredSmith—using Date.parse (or the Date constructor) for parsing strings is not recommended in general. While browsers are required to parse their own output, it's entirely implementation dependent (and some versions of IE got it wrong anyway), so they may not correctly parse the output of other browsers. It would help greatly if the next version of ECMAScript includes a new Date that has useful parsing and formatting. There are plenty of good libraries and other languages to show the way. – RobG Apr 11 '16 at 00:49

0 Answers0