Parse String dd-MMM-yyyy to Date Object JavaScript without Libraries

date, javascript, parsing

Solution

You need a simple parsing function like:

function parseDate(s) {
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
                jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
  var p = s.split('-');
  return new Date(p[2], months[p[1].toLowerCase()], p[0]);
}

console.log(parseDate('21-May-2014'));  //  Wed 21 May 00:00:00 ... 2014

Problem

For Example: ``` var dateStr = "21-May-2014"; ``` I want the above to be into a valid Date Object. Like, `var strConvt = new Date(dateStr);`

Original source

Related problems