Skip to main content
 首页 » 编程设计

c#之将十进制值转换为小时和分钟;还从另一个中减去两个小时/分钟值

2024年02月23日20飞鱼

我在 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) 

请您参考如下方法:

使用 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;