我在ASP.Net页面上有两个十进制文本框:
Balance: 200.00 (200 Hrs and 00 Minutes)
TextBox1 = 75.30 (75 Hrs and 30 Minutes)
(After entering the value in TextBox1; the function should calculate the difference between Balance - TextBox1)
TextBox2: (based on 60 min per hour) = 124.30 (124 Hrs and 30 minutes)
答案 0 :(得分:3)
使用TimeSpan
计算两个值之间的差异,但您必须分别传递小时和分钟。一般情况下,我会避免将时间跨度表示为十进制值,更常见的是您将冒号视为分隔符,即4:30。
//parse hours and minutes from textbox input
TimeSpan t1 = new TimeSpan(hours1, minutes1, 0);
TimeSpan t2 = new TimeSpan(hours2, minutes2, 0);
int deltaHours = (t1 - t2).Hours;
int deltaMinutes = (t1 - t2).Minutes;
答案 1 :(得分:1)
System.TimeSpan结构提供解析和转换时间的函数。它还可以让你对时间值进行算术运算。