Calculation of Total seconds from a particular format of time

asp.net, c#

Solution

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.

Problem

How to calculate total seconds of '33 hr 40 mins 40 secs' in asp.net c#

Original source