0

I have the following code:

var format_time="28-06-12 9:30:50";
var my_time_array=format_time.split(":");
alert(my_time_array[0]); // alerts 28-06-12 9
alert(my_time_array[1]); // alerts 30
alert(my_time_array[2]); // alerts 50

The first alert incorrectly displays "28-06-12 9", when I only want it to display "9". Additionally, if format_time="28-06-12 10:30:50", then I would want it to display "10".

I can't seem to work it out. Any ideas?

MMeah
1,0521 gold badge9 silver badges18 bronze badges
asked Jun 28, 2012 at 3:36

4 Answers 4

2

Split by space first:

var my_time_array=format_time.split(" ")[1].split(":");
answered Jun 28, 2012 at 3:40
Sign up to request clarification or add additional context in comments.

Comments

1

try

var hour = my_time_array[0].split(" ");
alert(hour[1]);
answered Jun 28, 2012 at 3:39

Comments

1

Try this:

var format_time="28-06-12 9:30:50"; 
var my_time_array=format_time.split(" "); 
var my_time_array2=my_time_array[1].split(":"); 
alert(my_time_array2[0]); // alerts 9 
alert(my_time_array2[1]); // alerts 30 
alert(my_time_array2[2]); // alerts 50 
answered Jun 28, 2012 at 3:41

Comments

0
var format_time="28-06-12 9:30:50"; 
var my_time_array1=format_time.split(" "); //Split on space. 
var my_time_array=my_time_array1[1].split(":"); //now split the time part.
alert(my_time_array[0]); // alerts 28-06-12 9 
alert(my_time_array[1]); // alerts 30 
alert(my_time_array[2]); // alerts 50 
answered Jun 28, 2012 at 3:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.