I need convert GMT time 14:21:34 to minute (14*60+21=861 minutes)
I know there is a inbuilt function which convert min to HH:MM
Use TimeSpan.FromMinutes
:
var result = TimeSpan.FromMinutes(1815);
Is there an easy way to do this that I am missing?
Two options to get to a TimeSpan
:
1) Parse it as a DateTime
(specifying a format), then use DateTime.TimeOfDay
to get a TimeSpan
var dt = DateTime.ParseExact(
"14:21:34",
@"HH\:mm\:ss",
CultureInfo.InvariantCulture);
var ts = dt.TimeOfDay;
I like this option as it sounds like the value you've got is meant to be a time of day.
2) Parse it as a TimeSpan
:
var ts = TimeSpan.ParseExact(
"14:21:34",
@"hh\:mm\:ss",
CultureInfo.InvariantCulture);
Once you have a TimeSpan
, use TotalMinutes
(which returns a double
) to get the number of minutes. For example:
Console.WriteLine((int) ts.TotalMinutes); // 861
See more on this question at Stackoverflow