WraithNath
WraithNath

Reputation: 18013

Convert decimal hours to DD:HH:MM

Im trying to convert a decmial number of hours to days, hours and minutes.

This is what I have so far, its not quite there yet. I need to subtract the number of hours from the days from the hours part if that makes sense?

        /// <summary>
        /// Converts from a decimal value to DD:HH:MM
        /// </summary>
        /// <param name="dHours">The total number of hours</param>
        /// <returns>DD:HH:MM string</returns>
        public static string ConvertFromDecimalToDDHHMM(decimal dHours)
        {
            try
            {
                decimal hours = Math.Floor(dHours); //take integral part
                decimal minutes = (dHours - hours) * 60.0M; //multiply fractional part with 60
                int D = (int)Math.Floor(dHours / 24);
                int H = (int)Math.Floor(hours);
                int M = (int)Math.Floor(minutes);
                //int S = (int)Math.Floor(seconds);   //add if you want seconds
                string timeFormat = String.Format("{0:00}:{1:00}:{2:00}", D, H, M);

                return timeFormat;
            }
            catch (Exception)
            {
                throw;
            }
        }

SOLUTION:

    /// <summary>
    /// Converts from a decimal value to DD:HH:MM
    /// </summary>
    /// <param name="dHours">The total number of hours</param>
    /// <returns>DD:HH:MM string</returns>
    public static string ConvertFromDecimalToDDHHMM(decimal dHours)
    {
        try
        {
            decimal hours = Math.Floor(dHours); //take integral part
            decimal minutes = (dHours - hours) * 60.0M; //multiply fractional part with 60
            int D = (int)Math.Floor(dHours / 24);
            int H = (int)Math.Floor(hours - (D * 24));
            int M = (int)Math.Floor(minutes);
            //int S = (int)Math.Floor(seconds);   //add if you want seconds
            string timeFormat = String.Format("{0:00}:{1:00}:{2:00}", D, H, M);

            return timeFormat;
        }
        catch (Exception)
        {
            throw;
        }
    }

Upvotes: 14

Views: 17962

Answers (6)

Zia Ur Rahman
Zia Ur Rahman

Reputation: 1880

Simple.

double counter = 0.25;
TimeSpan span = TimeSpan.FromMinutes(counter);
textbox1.Text = span.ToString(@"hh\:mm\:ss");

result will be 00:00:15 seconds. If counter = 1, then the result will be 00:01:00 and so on.

Upvotes: 2

user3442821
user3442821

Reputation: 1

public static string GetTimeString(Decimal dHours)
{
    DateTime dTime = new DateTime().AddHours(dHours);
    return dTime.ToString("HH:mm:ss");    // HH: 24h or hh: 12h
}

Upvotes: 0

Mayank
Mayank

Reputation: 8852

here is another post which expains it pretty good.

Convert date to string format yyyy-mm-dd HH:MM:SS - C#

MyDateTime.ToString("yyyy-MM-dd hh:mm:ss");

also

http://blog.stevex.net/string-formatting-in-csharp/

Upvotes: 0

Tom Chantler
Tom Chantler

Reputation: 14931

Why not do something like this?

double d = 25.23523;
Timespan t = TimeSpan.FromHours(d);

This will give you:

t = 1.01:14:06.8280000

Then you can interrogate the TimeSpan object as you wish: http://msdn.microsoft.com/en-us/library/system.timespan.aspx

NOTE: TimeSpan.FromHours needs a double input, not a decimal.

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460068

You could use TimeSpan.FromHours to get the timespan, then you have all you need:

TimeSpan ts = TimeSpan.FromHours(Decimal.ToDouble(dHours));

For example:

int D = ts.Days;
int H = ts.Hours;
int M = ts.Minutes;

Upvotes: 38

Jon Skeet
Jon Skeet

Reputation: 1500225

You need to subtract (D * 24) from hours... or you could just use:

int H = ((int) dHours) % 24;

If you're going to cast to int anyway, there's no need to call Math.Floor. So for example, you could actually use:

// I'd rename dHours as well, by the way...
int wholeHours = (int) dHours;
int days = wholeHours / 24;
int hours = wholeHours % 24;
int minutse = (int) ((dHours % 1M) * 60);

On the other hand, you need to be careful if it can be negative - all kinds of things could end up screwy in that case. If you don't believe you have to handle that, I'd explicitly check it and throw an exception if dHours is negative before you do anything else.

(Note that your try/catch block is pointless and distracting at the moment. Just get rid of it.)

Upvotes: 3

Related Questions