如何将double值转换为时间?C#/ ASP.NET

Jay*_*len 5 c#

如何将double价值转换为时间?例如,我有这个doubleval = 0.00295692867015203,我想返回4:15.

我做了很多研究,没有找到有效的解决方案!这是我尝试过的一个函数,但它返回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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用C#ASP.NET.我将不胜感激任何建议.

Jer*_*all 14

哇...花了我一会儿意识到你的意思"00:04:15.00"......

public static TimeSpan TimeSpan.FromDays(double value) 会得到你的 TimeSpan

DateTime.Today.AddDays(double value)会给你一个约会时间


Nol*_*rin 7

我想你想要的东西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
Run Code Online (Sandbox Code Playgroud)