[SOLVED] Convert hours to days in C#

Issue

I am pretty new to C# I have been researching but can not find specifically what I am looking for before I asked this question.

What is the best way to convert hours only to days in C?

If you know of any article or quick simple answer to point me to that would be great. I will keep looking though.

Thanks.

Solution

You could try using a TimeSpan struct. Wehave some static methods to make those conversions, like: TimeSpan.FromHours and it will return a new TimeSpan and we can read the TotalDays property. Or you could create a method for it:

public static double ConvertHoursToTotalDays(double hours)
{
    TimeSpan result = TimeSpan.FromHours(hours);
    
    return result.TotalDays;
}

And call it:

double days = ConvertHoursToTotalDays(120); // should be 5.0 

Answered By – Felipe Oriani

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *