Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Created August 31, 2014 21:08
Show Gist options
  • Save hagbarddenstore/6e3dede2b97ceec624da to your computer and use it in GitHub Desktop.
Save hagbarddenstore/6e3dede2b97ceec624da to your computer and use it in GitHub Desktop.
int ParseTime(string input)
{
var totalMinutes = 0;
input = Regex.Replace(input.ToLower(), "[^\\d|m|h]", string.Empty);
var hoursMatch = Regex.Match(input, "(?<Hours>\\d+)h");
if (hoursMatch.Success)
{
totalMinutes += int.Parse(hoursMatch.Groups["Hours"].Value) * 60;
}
var minutesMatch = Regex.Match(input, "(?<Minutes>\\d+)m");
if (minutesMatch.Success)
{
totalMinutes += int.Parse(minutesMatch.Groups["Minutes"].Value);
}
if ((!hoursMatch.Success && !minutesMatch.Success) || totalMinutes > (24 * 60))
{
throw new FormatException();
}
return totalMinutes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment