Friday, May 20, 2016

Get Total Hours and Minutes from total minutes using TimeSpan class

Given total number of minutes as input and you need to extract the total number of hours and remaining minutes out from the input, you need to use the TimeSpan class specifically FromMinutes() method.

C# Code
1
2
3
4
TimeSpan span = TimeSpan.FromMinutes(1506);
var hours = (int) span.TotalHours;
var minutes = span.Minutes;
Console.WriteLine(hours + ":" + minutes.ToString("D2"));

VB.NET Code
1
2
3
4
Dim span As TimeSpan = TimeSpan.FromMinutes(1506)
Dim hours = CInt(span.TotalHours)
Dim minutes = span.Minutes
Console.WriteLine(hours + ":" + minutes.ToString("D2"))

0 comments:

Post a Comment