How to get month value from month string in java script

 

var daysInMonth = function(year, month) {

return new Date(year, month+1, 0).getDate();

}

var convertStringToDate = function(dateInString, value) {

var monthName = dateInString.substring(0,3);

var year = dateInString.substring(4,8);

var months = [ “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”,  “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec” ];

var month = months.indexOf(monthName); var date;

if(value) {

date = new Date(year, month, 1);

} else {

date = new Date(year, month, daysInMonth(year, month));

} console.log(date);

return date;

}

 

convertStringToDate(“Jan-2017”, true);
convertStringToDate(“Feb-2017”, false);

Advertisements