如何将十进制数转换为时间,反之亦然

这是一个例子

if 8.30 is there it should be 8 hours 30 minute if 8 hour 20 minutes then 8.20 Please tell whether it is possible ? if yes how ? 

当人们谈论小时时,它们通常意味着0.1 = 6分钟。

因此,转换8.3的正确公式将是:

8小时+ 3 * 6分钟= 8:18

要将8:20转换为十进制,它将是:

8 + 20/6 = 8.333333(可能是8.3)

如果总是与之分开 你希望它显示然后只需使用:

 var ar="8.30".split(new[]{'.'}); Console.Write("{0} hours {1} minutes",ar[0], ar[1]); 

PS:这里我们肯定有数组中的两个元素,但请在使用ar[1]之前检查数组ar长度

我的方法看起来像这样。 (这是ruby,所以你必须自己转换它,但逻辑在这里很重要)

  def zeropad(number) return ((number.to_f < 10) ? "0" : "") + number.round.to_s end def decimal_to_time(value) t = value.split(".") #returns an array of ["hour", "minutes"] hours, minutes = t[0], t[1] minutes = zeropad( (minutes.to_f / 10**minutes.length) * 60 ) # parse the minutes into a time value return (minutes.to_i == 0) ? hours : hours + ":" + minutes end def findTime(value) value =~ /^\d+\.\d+/ ? decimal_to_time(value) : value end 

其中findTime(“5.015”)为您提供适当的时间值。

我在以下测试中对此进行了测试,它们都通过了。

  | entered_time | expected_results| | "5.6" | "5:36" | | "5.9" | "5:54" | | "5.09" | "5:05" | | "5.0" | "5" | | "5.00" | "5" | | "5.015" | "5:01" | | "6.03" | "6:02" | | "5.30" | "5:18" | | "4.2" | "4:12" | | "8.3" | "8:18" | | "8.33" | "8:20" | | "105.5" | "105:30" | | "16.7" | "16:42" | | "Abc" | "Abc" | | "5:36" | "5:36" | | "5:44" | "5:44" | 

这里有几个扩展方法(对于DateTime和Decimal)来完成这项工作:

 public static class DecimalToTimeConverters { public static DateTime ToDateTime(this decimal value) { string[] parts = value.ToString().Split(new char[] { '.' }); int hours = Convert.ToInt32(parts[0]); int minutes = Convert.ToInt32(parts[1]); if ((hours > 23) || (hours < 0)) { throw new ArgumentOutOfRangeException("value", "decimal value must be no greater than 23.59 and no less than 0"); } if ((minutes > 59) || (minutes < 0)) { throw new ArgumentOutOfRangeException("value", "decimal value must be no greater than 23.59 and no less than 0"); } DateTime d = new DateTime(1, 1, 1, hours, minutes, 0); return d; } public static Decimal ToDecimal(this DateTime datetime) { Decimal d = new decimal(); d = datetime.Hour; d = d + Convert.ToDecimal((datetime.Minute * 0.01)); return d; } } 

我在一个新的空白页面中使用以下内容在ASP.net网页(我当时打开了一个Web项目)中非常快速地测试了这一点,它看起来很有效:

 protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Decimal d = new decimal(); d = 3.45M; Response.Write(d.ToDateTime().ToString()); Response.Write("
"); DateTime d2 = new DateTime(2009, 1, 1, 4, 55, 0); Response.Write(d2.ToDecimal().ToString()); }

按照Rob而不是替补

 string[] parts = value.ToString().Split(new char[] { '.' }); int hours = Convert.ToInt32(parts[0]); int minutes = Convert.ToInt32(parts[1]); 

 int hours = (int)value; int minutes = (int)((value - minutes) * 100); 

没有字符串或依赖当前文化(假设’。’是小数点)

如何将txtDuration.Text值解析为十进制值?

 if (txtDuration.Text) { var duration = int.Parse(txtDuration.Text); var timespan = Boolean.Parse(hdfToggleDuration.Value) ? new TimeSpan (0, 0, duration, 0) : new TimeSpan (0, duration, 0, 0); DateTime end = start.Add(timespan); }