How can I convert a HH:mm:ss string to a JavaScript Date object?

javascript, jquery

Solution

Try this (without jQuery and a date object (it's only a time)):

var
    pieces = "8:19:02".split(':')
    hour, minute, second;

if(pieces.length === 3) {
    hour = parseInt(pieces[0], 10);
    minute = parseInt(pieces[1], 10);
    second = parseInt(pieces[2], 10);
}

Problem

I have dynamic string with a `HH:mm:ss` format (e.g. `18:19:02`). How can the string be converted into a JavaScript Date object (in Internet Explorer 8, Chrome, and Firefox)? I tried the following: ``` var d = Date.parse("18:19:02"); document.write(d.getMinutes() + ":" + d.getSeconds()); ```

Original source