Hello all. Can anyone show me the best way to convert a duration consisting of only minutes and seconds (such as 2:15) into all seconds (which would be 135 based on my original 2:15). Thanks in advance!
slrobinson1983
0
Newbie Poster
Recommended Answers
Jump to Postfunction toSeconds(str){ str=str.split(':'); switch( str.length ) { case 2: return str[0]*60 + str[1]*1; case 3: return str[0]*3600 + str[1]*60 + str[2]*1; } } alert( toSeconds("2:15") )
Jump to PostYou could explode the string using the ":" as the delimiter. This will give you an array with two entries. Take the first entry, multiply it by 60 then add the result to the second entry and you're done.
All 5 Replies
hielo
65
Veteran Poster
chrishea
182
Nearly a Posting Virtuoso
karthik_ppts
81
Posting Pro
hielo
65
Veteran Poster
slrobinson1983
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.