1

How I can convert string "29-05-2014" to date format in js? I tried the code given below but it shows an invalid date error message.

var date_text = $(".startdate").text(); 
var start_date = new Date(date_text);

Please help me. Thanks in advance.

Anna
  • 973
  • 4
  • 13
  • 26

2 Answers2

1

You may try using this:

var parseDate = function(txt)
{
    console.log(txt);
    txt = txt.replace(/(\d{2})\-(\d{2})\-(\d{4})/, '$2/$1/$3');
    console.log(txt);
    return new Date(txt);
};
alert(parseDate("29-05-2014"));
alert(parseDate("16-06-2014"));
0

You should use yyyy-mm-dd date format. This works:

 a = new Date("2014-05-14");
maxijb
  • 541
  • 4
  • 13