How to calculate total seconds of '33 hr 40 mins 40 secs' in asp.net c#
new TimeSpan(33, 40, 40).TotalSeconds;
If you're given a string of the format "33 hr 40 mins 40 secs"
, you'll have to parse the string first.
var s = "33 hr 40 mins 40 secs";
var matches = Regex.Matches(s, "\d+");
var hr = Convert.ToInt32(matches[0]);
var min = Convert.ToInt32(matches[1]);
var sec = Convert.ToInt32(matches[2]);
var totalSec = hr * 3600 + min * 60 + sec;
That code, obviously, has no error checking involved. So you might want to do things like make sure that 3 matches were found, that the matches are valid values for minutes and seconds, etc.
Separate hour, minute and seconds and then use
Edited
TimeSpan ts = new TimeSpan(33,40,40);
/* Gets the value of the current TimeSpan structure expressed in whole
and fractional seconds. */
double totalSeconds = ts.TotalSeconds;
Read TimeSpan.TotalSeconds Property
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With