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?

4 Answers 4

2

Split by space first:

var my_time_array=format_time.split(" ")[1].split(":");
Sign up to request clarification or add additional context in comments.

Comments

1

try

var hour = my_time_array[0].split(" ");
alert(hour[1]);

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 

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 

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.