Just write down the part of the date and month, replace them and execute Date.parseand new Datehow it
function getFormattedDate(dateString) {
var result = dateString.replace(/(\d+)\/(\d+)(.*)/, function (m, g1, g2, g3) {
return g2 + "/" + g1 + g3;
});
return new Date(Date.parse(result));
}
console.log(getFormattedDate('1/11/2014 13:42:54'));
(\d+)\/(\d+)(.*) , (\d+) , / (escaped as \/), - (\d+) (.*). , g2 g1 ( ).
. , , , Date. Date.parse, ,
function getEpochTime(dateString) {
var result = dateString.replace(/(\d+)\/(\d+)(.*)/, function (m, g1, g2, g3) {
return g2 + "/" + g1 + g3;
});
return Date.parse(result);
}
function comparator(firstDate, secondDate) {
return getEpochTime(firstDate) - getEpochTime(secondDate);
}
:
var arr = ['3/11/2014 13:42:54',
'2/11/2014 13:42:54',
'1/12/2014 13:42:54',
'1/11/2014 13:43:54'
];
arr.sort(comparator);
console.log(arr);
[ '1/11/2014 13:43:54',
'2/11/2014 13:42:54',
'3/11/2014 13:42:54',
'1/12/2014 13:42:54' ]