tags:

views:

298

answers:

5

I want to parse a Date chosen by user:

var ds = "11 / 08 / 2009";

I use

var d = new Date(ds);

It gives me November, 08, 2009. But what I need is August, 11, 2009.

What is the simplest way to parse the date?

+15  A: 

There are lots of libraries and copy-and-paste javascript snippets on the net for this kind of thing, but here is one more.

function dateParse(s) {
  var parts = s.split('/');
  var d = new Date( parts[2], parts[1]-1, parts[0]);
  return d;
}
Licky Lindsay
+1  A: 

Extend date to return values in your desired format.

Here is a great article on how to do so. It includes code snippets.

Jim Schubert
+2  A: 

I have had success with DateJS. In particular, you would want to use parseExact, which allows you to provide a format string describing the meaning of each segment (so that you can map one segment to day and another to month).

Daniel Yankowsky
A: 

This article shows ways to convert date format from one to the other.

Saeros
A: 

Just another option (maybe not the best), which I wrote:

DP_DateExtensions Library

Supports date/time parsing and formatting (using related CFML function as a model), date math (add/subtract by date part), comparison, etc.

Jim Davis