Skip to main content

Ivan Teoh

Something personal yet public

167-exercise46.js

(Source)

function extractDate(paragraph) {
    var colon = paragraph.indexOf(":");
    var space = paragraph.indexOf(" ");
    var allDate = paragraph.slice(space + 1, colon);
    var dates = allDate.split("/");
    var day = dates[0];
    var month = dates[1];
    var year = dates[2];
    var result = new Date(year, month-1, day);
    return result;
}
var paragraph = "died 27/04/2006: Black Leclère";
document.write(extractDate(paragraph));