3

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!

4
  • 6
    what a ridiculous question. You do realise that jQuery isn't a language right? Commented Jun 19, 2010 at 4:19
  • 4
    @SpliFF While you are right, I think a beginner may not know the difference. Commented Jun 19, 2010 at 4:23
  • Especially based on how often they are told to use it on sites like this. 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. Commented Jun 19, 2010 at 13:04

2 Answers 2

10
var time = "00:00:00";
var parts = time.split(':');

alert("hours: " + parts[0] + ", minutes: " + parts[1] + ", seconds: ", + parts[2])
Sign up to request clarification or add additional context in comments.

3 Comments

I question your use of document.write in this day and age, but +1 for correct methodology.
I couldn't think of a better quick example of usage.
alert? less likely to interfere with anything they already have on the page.
4

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.