views:

91

answers:

2

I'd like to parse the following string which is a time (HH:MM:SS): 00:00:00

Does anyone know how I can get the Hour, Minute, or Seconds values?

Thank you!

+9  A: 
var time = "00:00:00";
var parts = time.split(':');

alert("hours: " + parts[0] + ", minutes: " + parts[1] + ", seconds: ", + parts[2])
halkeye
I question your use of document.write in this day and age, but +1 for correct methodology.
Jamie Wong
I couldn't think of a better quick example of usage.
halkeye
@halkeye alert? less likely to interfere with anything they already have on the page.
Jamie Wong
good point/idea, I've changed the answer to that.
halkeye
Thanks a lot... perfect!
Dave
+4  A: 

I'd probably go with the split(':') solution myself, but here's an interesting alternative using the native Date parsing:

var time = '00:23:54';

var date = new Date('1/1/1900 ' + time);

// 0
date.getHours();

// 23
date.getMinutes();

// 54
date.getSeconds();
Dave Ward
+1 Wouldn't have thought of that - cool stuff.
Jamie Wong
+1 neat! Probably more reliable than regex'ing too
alex