在.net中将时间转换为小数

发布于 2024-08-31 08:49:16 字数 343 浏览 2 评论 0原文

有没有一种简单的方法将时间 (hh:mm) 显示为十进制值? 例如,01:08 应变为 1,13。

我有一个 asp:textbox 掩码(ajax)为时间,掩码格式为“99:99”。在此框旁边,我需要将输入的值显示为十进制。

<asp:TextBox ID="time" runat="server" /> hh:mm ([time in decimal format])
<ajaxToolkit:MaskedEditExtender runat="server" Mask="99:99" TargetControlID="time" MaskType="Time" />

Is there an easy way to present time (hh:mm) as a decimal value?
Example, 01:08 should become 1,13.

I have an asp:textbox masked (ajax) as time with the mask format "99:99". Next to this box I need to present the entered value as decimal.

<asp:TextBox ID="time" runat="server" /> hh:mm ([time in decimal format])
<ajaxToolkit:MaskedEditExtender runat="server" Mask="99:99" TargetControlID="time" MaskType="Time" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白色秋天 2024-09-07 08:49:16

您可以使用 TimeSpan 的 TotalHours 属性,如下所示:

DateTime endHour = new DateTime(2010, 1, 2, 5, 30, 0);

double totalHours = new TimeSpan(endHour.Hour, endHour.Minute, 0).TotalHours;

You can use the TotalHours property of the TimeSpan, like this:

DateTime endHour = new DateTime(2010, 1, 2, 5, 30, 0);

double totalHours = new TimeSpan(endHour.Hour, endHour.Minute, 0).TotalHours;
落花随流水 2024-09-07 08:49:16

首先,您可能会发现使用 TimeSpan 而不是 DateTime 更合适。因此,对于您的示例,这:

TimeSpan t = new TimeSpan(0, 1, 8);
decimal d = t.Minutes + (t.Seconds / 60m);
Console.WriteLine(d.ToString());

产生正确的结果。 60 后面的 m 强制计算结果为十进制。

您无法将此 C# 直接挂钩到文本框的 onblur,因为它运行在服务器端。如果您需要这个来运行客户端,您将必须使用 ajax 回调来评估它,或者使用 javascript 来剪切字符串,然后单独计算每个位(分钟和秒),然后将结果输出到第二个文本框或标签。

First up, you might find it more appropriate to use a TimeSpan rather than a DateTime. So for your example, this:

TimeSpan t = new TimeSpan(0, 1, 8);
decimal d = t.Minutes + (t.Seconds / 60m);
Console.WriteLine(d.ToString());

produces the correct result. The m after the 60 forces that calculation to be decimal.

You can't hook this C# directly to the onblur of the textbox, as it runs server side. If you need this to run client side you will have to either use an ajax callback to evaluate it, or use javascript to cut the string up, then calculate each bit (minutes and seconds) individually, then output the result to the second textbox or label.

苏璃陌 2024-09-07 08:49:16
double decimalTime = DateTime.Now.Hour + (double)DateTime.Now.Minute/60

编辑:更好的方法,使用 GenEric 的 TimeSpan 想法:

double hours = new TimeSpan(DateTime.Now.Hour, 
                            DateTime.Now.Minute, 
                            0).TotalHours;
double decimalTime = DateTime.Now.Hour + (double)DateTime.Now.Minute/60

Edit: Better way, using GenEric's TimeSpan idea:

double hours = new TimeSpan(DateTime.Now.Hour, 
                            DateTime.Now.Minute, 
                            0).TotalHours;
成熟稳重的好男人 2024-09-07 08:49:16
(CDate("01:08").TimeOfDay.TotalHours).ToString("N2"))
(CDate("01:08").TimeOfDay.TotalHours).ToString("N2"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文