简体   繁体   English

使用TimeSpan计算时差

[英]Calculate time difference using TimeSpan

I am trying to calculate the time difference of two given times slots but the answers does not seem to be correct what am I doing wrong? 我正在尝试计算两个给定时隙的时差,但答案似乎不正确,我在做什么错?

My code: 我的代码: 在此处输入图片说明

For some reason the value given amFinish is changed from 16:30 to 16:18:00 I have no idea why! 由于某些原因, amFinish的值从16:30更改为16:18:00,我不知道为什么!

And What if I have a text box and the user enters 16.30 how would I take that value and compute it as 16hrs and 30mins 而且如果我有一个文本框并且用户输入16.30怎么办,我将如何获取该值并将其计算为16小时30分钟

The answer should be 05.30 but instead I get 05.18. 答案应该是05.30,但我得到的是05.18。 Any sugestions? 有任何建议吗?

30% of an hour is 18 minutes. 每小时30%是18分钟。

16.30 hours is 16 hours and 30/100 parts of an hour. 16.30小时是16小时和30/100小时。

16 hours and a half would be 16.50. 16小时半将是16.50。

You have decimal 16.3 hours, which is 16 hours and 18 minutes as Oded explains. 您的小数位数为16.3小时,如Oded所述,为16小时18分钟。

If you need to specify both hours, minutes, and seconds, use the overload of the TimeSpan constructor which takes three arguments: 如果需要同时指定小时,分钟和秒,请使用TimeSpan构造函数的重载,该构造函数TimeSpan三个参数:

TimeSpan amStart = new TimeSpan(0, 11, 0);
TimeSpan amFinish = new TimeSpan(16, 30, 0);

And if you need to convert a string into a TimeSpan , use something like: 如果需要将string转换为TimeSpan ,请使用类似以下内容的方法:

TimeSpan amFinish = TimeSpan.ParseExact("16.30", @"hh\.mm", CultureInfo.InvariantCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM