An alternative to the split method is to use lastIndexof and slice instead to change the year to the ISO8601 format, which then yields a non-standard string that is known to work in all browsers and then uses the date analysis method . (assuming a fixed pattern, as in the question)
but
If you want to make sure how it is disassembled, you must do it yourself and feed the individual parts to the constructor:
this will mean using the split method, see @Bergi answer .
var string = "04/06/13", index = string.lastIndexOf("/") + 1, date = new Date(string.slice(0, index) + (2000 + parseInt(string.slice(index), 10))); console.log(date);
Output
Sat Apr 06 2013 00:00:00 GMT+0200 (CEST)
Jsfiddle on
or another alternative would be to use moments.js
var string = "04/06/13"; console.log(moment(string, "DD/MM/YY").toString());
Output
Sat Apr 06 2013 00:00:00 GMT+0200 (CEST)
Jsfiddle on
source share