Skip to content Skip to sidebar Skip to footer

Parse String Dd-MMM-yyyy To Date Object JavaScript Without Libraries

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

Solution 1:

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

Post a Comment for "Parse String Dd-MMM-yyyy To Date Object JavaScript Without Libraries"