Just capture the date and month part, swap them and do Date.parse and new Date, like this
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'));
// Sat Nov 01 2014 13:42:54 GMT+0000 (GMT)
Here, the regular expression, (\d+)\/(\d+)(.*) will capture three parts of the string, first (\d+) captures the date part followed by / (escaped as \/) and the month part with another (\d+) and the rest of the string is captured with (.*). Then we return a new string by swapping the positions of g2 and g1 (month and date part).
Note: If all you are trying to do is sorting, then you don't need to create a new Date object. You can simply use the result of Date.parse which is epoch time, like this
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);
}
and then sort like this
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);
would give you
[ '1/11/2014 13:43:54',
'2/11/2014 13:42:54',
'3/11/2014 13:42:54',
'1/12/2014 13:42:54' ]