As you stated the problem, I had a solution, tweak it as you want.
Also try this Fiddle DEMO
function dateFormater(dateStr, format, separator) {
var date = new Date(dateStr),
formatArr = format.split('-'),
len = formatArr.length,
str = '',
i,
monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
],
getdata = function(d) {
function getCurrentTime(d) {
var currentTime,
hour = d.getHours(),
meridiem = hour >= 12 ? "PM" : "AM";
return ((hour + 11) % 12 + 1) + ":" + d.getMinutes() + ' ' + meridiem;
}
switch (d) {
case 'day':
return getDay(date.getDay);
case 'month':
return monthNames[date.getMonth()];
case 'dd':
return date.getDate();
case 'mm':
return date.getMonth();
case 'yy':
return date.getFullYear();
case 'time':
return getCurrentTime(date);
case 'zone':
return date.toString().match(/\(([A-Za-z\s].*)\)/)[1];
}
},
getDay = function(d) {
switch (d) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
}
};
for (i = 0; i < len; i++) {
i === len - 1 && (separator = '');
str += getdata(formatArr[i]) + separator;
}
return str;
}
var format = 'month-dd-yy-time-zone';
console.log(dateFormater("24 May 2017, 05:35", format, ', ')); // May, 24, 2017, 5:35 AM, IST