function formatDate() {
var now = new Date();
var month = '' + (now.getUTCMonth() + 1);
var day = '' + now.getUTCDate();
var year = now.getUTCFullYear();
var hour = '' + now.getUTCHours();
var minute = '' + now.getUTCMinutes();
var second = '' + now.getUTCSeconds();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
if (hour.length < 2) hour = '0' + hour;
if (minute.length < 2) minute = '0' + minute;
if (second.length < 2) second = '0' + second;
return [year, month, day].join('-')
+ ' ' + [hour, minute, second].join(':');
}
function stringToDate(dateString, format, delimeter) {
var formatLowerCase = format.toLowerCase();
var formatItems = formatLowerCase.split(delimeter);
var dateItems = dateString.split(delimeter);
var monthIndex = formatItems.indexOf("mm");
var dayIndex = formatItems.indexOf("dd");
var yearIndex = formatItems.indexOf("yyyy");
var month = parseInt(dateItems[monthIndex]) - 1;
return new Date(dateItems[yearIndex], month, dateItems[dayIndex]);
}
stringToDate("27/2/2018", "dd/MM/yyyy", "/");
stringToDate("2/27/2018", "mm/dd/yyyy", "/");
stringToDate("2-27-2018", "mm-dd-yyyy", "-");