Convert minutes to full time C#

22,983

Solution 1

Use TimeSpan.FromMinutes:

var result = TimeSpan.FromMinutes(1815);

This will give you an object that you can use in different ways.
For example:

var hours = (int)result.TotalHours;
var minutes = result.Minutes;

Solution 2

you can use this function


//minutes to be converted (70minutes = 1:10 hours)
int totalminutes = 70;
//total hours
int hours = 70 / 60;
//total minutes
int minutes = 70 % 60;
//output is 1:10
var time = string.Format("{0} : {1}", hours, minutes);

Solution 3

Try TimeSpan.FromMinutes(minutes), this will give you TimeSpan, after that you can check TimeSpan.Hours and TimeSpan.Minutes properties.

Solution 4

        DateTime d = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
        Console.WriteLine(d.ToLongTimeString());
        Console.WriteLine(d.AddMinutes(1815).ToLongTimeString());
        Console.ReadLine();
Share:
22,983
soamazing
Author by

soamazing

Updated on July 04, 2020

Comments

  • soamazing
    soamazing almost 4 years

    I need convert 1815 minutes to 30:15 (30 hours and 15 minutes)

    Is there an easy way to do this that I am missing?

  • Rochelle C
    Rochelle C about 10 years
    What is the easiest way to get this into a string? If the minutes are 65, this would give us 1 and 5 or 1:5 which doesn't look right. I could do an if statement to check if minutes is less than 10 but is there a more elegant way?
  • Daniel Hilgarth
    Daniel Hilgarth about 10 years
    @chill182: result.ToString() should work. You could also customize that by using format strings: result.ToString(@"hh\:mm");