在 C# 中获取两次之间的时间跨度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12521366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting time span between two times in C#?
提问by Hunter Mitchell
I have two textboxes. One for a clock in time and one for clock out. The times will be put in this format:
我有两个文本框。一个用于计时,一个用于计时。时间将采用以下格式:
Hours:Minutes
Lets say I have clocked in at 7:00 AM and clocked out at 2:00 PM.
假设我在早上 7:00 打卡并在下午 2:00 打卡。
With my current code, I get a difference of 2 hours, but it should be 7 hours. How would I do that in C#. I was going to convert to the 24 hour, by letting the user select AM or PM, but I got confused.
使用我当前的代码,我得到了 2 小时的差异,但应该是 7 小时。我将如何在 C# 中做到这一点。我打算通过让用户选择 AM 或 PM 来转换为 24 小时制,但我感到困惑。
So, basically, how would I calculate the difference of hours between the two times?
那么,基本上,我将如何计算两次之间的小时差?
I tried this, but got 2 hours and not 7 when I plugged in the numbers.
我试过这个,但是当我输入数字时得到了 2 小时而不是 7 小时。
DateTime startTime = Convert.ToDateTime(textBox1.Text);
DateTime endtime = Convert.ToDateTime(textBox2.Text);
TimeSpan duration = startTime - endtime;
采纳答案by Kittoes0124
string startTime = "7:00 AM";
string endTime = "2:00 PM";
TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
Console.WriteLine(duration);
Console.ReadKey();
Will output: 07:00:00.
将输出:07:00:00。
It also works if the user input military time:
如果用户输入军用时间,它也有效:
string startTime = "7:00";
string endTime = "14:00";
TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
Console.WriteLine(duration);
Console.ReadKey();
Outputs: 07:00:00.
输出:07:00:00。
To change the format:duration.ToString(@"hh\:mm")
要更改格式:duration.ToString(@"hh\:mm")
More info at:http://msdn.microsoft.com/en-us/library/ee372287.aspx
更多信息请访问:http : //msdn.microsoft.com/en-us/library/ee372287.aspx
Addendum:
附录:
Over the years it has somewhat bothered me that this is the most popular answer I have ever given; the original answer never actually explained why the OP's code didn't work despite the fact that it isperfectly valid. The only reason it gets so many votes is because the post comes up on Google when people search for a combination of the terms "C#", "timespan", and "between".
多年来,这是我给出的最受欢迎的答案,这让我有些困扰;原来的答案从来没有真正解释了为什么OP的码并未尽管事实上,它的工作是非常有效的。它获得如此多选票的唯一原因是当人们搜索术语“C#”、“时间跨度”和“之间”的组合时,该帖子会出现在 Google 上。
回答by StriplingWarrior
Two points:
两点:
Check your inputs. I can't imagine a situation where you'd get 2 hours by subtracting the time values you're talking about. If I do this:
DateTime startTime = Convert.ToDateTime("7:00 AM"); DateTime endtime = Convert.ToDateTime("2:00 PM"); TimeSpan duration = startTime - endtime;... I get
-07:00:00as the result. And even if I forget to provide the AM/PM value:DateTime startTime = Convert.ToDateTime("7:00"); DateTime endtime = Convert.ToDateTime("2:00"); TimeSpan duration = startTime - endtime;... I get
05:00:00. So either your inputs don't contain the values you have listed or you are in a machine environment where they are begin parsed in an unexpected way. Or you're not actually getting the results you are reporting.To find the difference between a start and end time, you need to do
endTime - startTime, not the other way around.
检查您的输入。我无法想象通过减去您正在谈论的时间值来获得 2 小时的情况。如果我这样做:
DateTime startTime = Convert.ToDateTime("7:00 AM"); DateTime endtime = Convert.ToDateTime("2:00 PM"); TimeSpan duration = startTime - endtime;......我得到
-07:00:00了结果。即使我忘记提供 AM/PM 值:DateTime startTime = Convert.ToDateTime("7:00"); DateTime endtime = Convert.ToDateTime("2:00"); TimeSpan duration = startTime - endtime;...我明白了
05:00:00。因此,要么您的输入不包含您列出的值,要么您处于以意外方式开始解析它们的机器环境中。或者您实际上并没有得到您报告的结果。要找到开始时间和结束时间之间的差异,您需要执行
endTime - startTime,而不是相反。
回答by Tim Lehner
回答by SMA
Another way ( longer ) In VB.net [ Say 2300 Start and 0700 Finish next day ]
另一种方式(更长)在 VB.net [说 2300 开始和 0700 第二天完成]
If tsStart > tsFinish Then
如果 tsStart > tsFinish 那么
' Take Hours difference and adjust accordingly
tsDifference = New TimeSpan((24 - tsStart.Hours) + tsFinish.Hours, 0, 0)
' Add Minutes to Difference
tsDifference = tsDifference.Add(New TimeSpan(0, Math.Abs(tsStart.Minutes - tsFinish.Minutes), 0))
' Add Seonds to Difference
tsDifference = tsDifference.Add(New TimeSpan(0, 0, Math.Abs(tsStart.Seconds - tsFinish.Seconds)))

