Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to convert HH:MM:SS (hours:minutes:seconds.split seconds) to seconds

What's an easy way to convert 00:20:40.28 (HH:MM:SS) to seconds with a Bash script?

Split seconds can be cut out, it’s not essential.

like image 555
Mint Avatar asked Feb 02 '10 03:02

Mint


People also ask

How do you convert HH mm SS to SEC?

To convert hh:mm:ss to seconds:Convert the hours to seconds, by multiplying by 60 twice. Convert the minutes to seconds by multiplying by 60 .

How do you convert HH mm SS to minutes?

To convert hours and minutes to minutes, you have to multiply the hh:mm value by 1440 (which is 24 [number of hours in the day] multiplied by 60 [number of minutes in an hour]), AND make sure you set the formatting correctly for the both the hh:mm cells and the resulting minute cells.


1 Answers

Try awk. As a bonus, you can keep the split seconds.

echo "00:20:40.25" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }' 
like image 184
dreamlax Avatar answered Sep 30 '22 15:09

dreamlax