1

I have string as 06/08/2013 that I want to convert into Date Object

I do

var transactionDate = Date.parse(transactionDateAsString);

and I get NaN

How can I tell javascript to format the string as dd/mm/yyyy?

daydreamer
  • 80,741
  • 175
  • 429
  • 691

2 Answers2

4

Break it down and spoon-feed it:

var parts = transactionDateAsString.split("/");
var date = new Date(parts[2],parts[1]-1,parts[0]);
Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
0

Date.parse() from the docs:

Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

You probably want to use the Date constructor:

var transactionDate = new Date(transactionDateAsString);
Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
David Hellsing
  • 102,045
  • 43
  • 170
  • 208
  • That makes it `Sat Jun 08 2013 00:00:00 GMT-0700 (PDT)` which is incorrect. It should be `August 06` – daydreamer Aug 06 '13 at 21:13
  • @daydreamer: The native parse functions use the American format `MM/DD/YYYY`. At least it's consistent across browsers. – Bergi Aug 06 '13 at 21:16