Convert string timer to integer seconds
i'm trying to convert an string like "0:13:30", which consist of h:mm:ss, to an integer which will be an answer of (m*60)+(s), working only with the minutes and seconds in greasemonkey or jscript. What i curently have is:
var t_str = ''; var t_int =0;
var str1='';var str2='';var t_int1=0;var t_int2=0;
t_str="0:13:30";
alert(t_str);
str1=t_str[4]+t_str[5];
str2=t_str[2]+t_str[3];
t_int1=parseInt (str1);
t_int2=parseInt (str2);
t_int2=t_int2 * 60;
t_int=t_int1+t_int2;
alert(t_int);
I get up to the first alert. how do i get it to as开发者_如何学编程sign the values "13" and "30" to str2 and str1? Sorry for a basic question, but im not used to this language :)
var time = t_str.split(":"),
h = 3600 * parseInt(time[0], 10),
m = 60 * parseInt(time[1], 10),
s = parseInt(time[2], 10);
alert(h+m+s);
I assume you are using javascript. If yes, then you can use split() method to split the t_str first and then carry on with your parsing of integers.
`str = t_str.split(":");` //- array of {0,13,30}
and then use this array to access your numbers.
精彩评论