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!
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!
Share Improve this question edited Jun 19, 2010 at 4:20 alex 490k204 gold badges889 silver badges991 bronze badges asked Jun 19, 2010 at 4:15 dzmdzm 23.6k50 gold badges152 silver badges229 bronze badges 4- 6 what a ridiculous question. You do realise that jQuery isn't a language right? – SpliFF Commented Jun 19, 2010 at 4:19
- 4 @SpliFF While you are right, I think a beginner may not know the difference. – alex Commented Jun 19, 2010 at 4:23
- Especially based on how often they are told to use it on sites like this. – Aaron Butacov Commented Jun 19, 2010 at 5:57
- Yes, I was unaware of JavaScripts split method - I mention jQuery simply because that's the framework I use. – dzm Commented Jun 19, 2010 at 13:04
2 Answers
Reset to default 10var time = "00:00:00";
var parts = time.split(':');
alert("hours: " + parts[0] + ", minutes: " + parts[1] + ", seconds: ", + parts[2])
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();