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
  • 87,243
  • 191
  • 450
  • 722

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
  • 320,036
  • 81
  • 464
  • 592
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
  • 795,719
  • 175
  • 1,089
  • 1,143
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • 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