-2

My objective to get the date in day month year format from a textbox and convert it to mm/dd/yyyy format so that i can compare dates

var userDate = document.getElementById("dateOfJourney").value; //eg 21/05/2013
var formattedUserDate = new Date( userDate.replace( /(\d{2})\/(\d{2})\/(\d{4})/, "$2/$1/$3"));
alert(formattedUserDate);

i'm trying to escape 'forward slash' but i'm getting invalid date output eg. 05/21/2013

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

You could have done like.

var userDate = "21/5/2013".split("/");
var newDate = userDate[1]+"/"+userDate[0]+"/"+userDate[2];
var formattedUserDate = new Date(newDate);
alert(formattedUserDate);

instead of using the regex. Here is a working sample.

Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101