Parse String dd-MMM-yyyy to Date Object JavaScript

2020-02-14 07:39发布

For Example:

var dateStr = "21-May-2014";

I want the above to be into a valid Date Object.

Like, var strConvt = new Date(dateStr);

1条回答
够拽才男人
2楼-- · 2020-02-14 08:03

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
查看更多
登录 后发表回答