How to convert double value at a time? C # / ASP.NET

How to convert value doublein time? for example, I have this value double val = 0.00295692867015203and I want to return it 4:15.

I did a lot of research and didn't find a solution that worked! Here is a function that I tried too, but returns 00:00:00:

ConvertFromDecimalToDDHHMM(Convert.ToDecimal(val));

public 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;
    }
}

I use C#and ASP.NET. I would appreciate any advice.

+5
source share
3 answers

Wow ... took me to understand what you meant "00:04:15.00"....

public static TimeSpan TimeSpan.FromDays(double value) will provide you TimeSpan

And DateTime.Today.AddDays(double value)give you the date

+14
source

, - TimeSpan.FromDays(0.00295692867015203). TimeSpan double TimeSpan (): http://msdn.microsoft.com/en-us/library/system.timespan.fromdays.aspx.

TimeSpan -, :

var now = DateTime.Now; // say, 25/13/2013 12:23:34
var interval = TimeSpan.FromDays(0.00295692867015203); // 4:15
var futureTime = now + interval; // 25/13/2013 12:27:49
+7

You must try

public static string ConvertFromDoubleToDDHHMM(double days) {
  return TimeSpan.FromDays(days).ToString(@"dd\:hh\:mm");
}

First, it creates a structure value TimeSpan, and then formats that value as string. For more information, see Custom TimeSpan format strings on MSDN .

Works for .NET 4.0 and later.

0
source

All Articles