C# 获取两个值之间的时间差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10500095/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:05:51  来源:igfitidea点击:

Getting time difference between two values

c#.netdatetime

提问by PandaNL

In my application I have 4 TextBoxes, and 2 TextBoxes to enter the start-time, and 2 TextBoxes to enter end-time.

在我的应用程序中,我有 4 个文本框和 2 个用于输入开始时间的文本框,以及 2 个用于输入结束时间的文本框。

The user will always enter a completed time, so the input will always be 11:30, 12:45, and so on.

用户将始终输入已完成的时间,因此输入将始终为 11:30、12:45 等。

How can I get the difference in hours and minutes between start and endtime?

如何获得开始时间和结束时间之间的小时和分钟差异?

采纳答案by Mitja Bonca

Use TimeSpan class, and Subtract method of DateTime.

使用 TimeSpan 类和 DateTime 的 Subtract 方法。

        DateTime t1 = Convert.ToDateTime(textBox1.Text);
        DateTime t2 = Convert.ToDateTime(textBox2.Text);
        TimeSpan ts = t1.Subtract(t2);

回答by RhysW

convert the hours to minutes, add it to the existing minutes, convert those down to total seconds, do the same with endtime. minus them from each other, convert them back up to hours and minutes. remembering 60 minutes in an hour, 60 seconds in a minute, thats how i would deal with it. because total seconds will always be the same, its hard trying to teach a computer trying to wrap around from 60 back to 0 or from 12 to 1. much easier to use somethign perfectly linear like seconds. then reconvert upwards

将小时转换为分钟,将其添加到现有分钟,将它们转换为总秒数,对 endtime 执行相同操作。将它们相互减去,将它们转换回小时和分钟。一小时内记住60分钟,一分钟内记住60秒,这就是我将如何处理它。因为总秒数总是相同的,所以很难教计算机尝试从 60 回到 0 或从 12 到 1。使用像秒这样完美线性的东西要容易得多。然后向上转换

回答by Icarus

TimeSpan difference = DateTime.Parse(txtbox1.Text) - Datetime.Parse(txtbox2.Text);

Example:

例子:

TimeSpan difference =  DateTime.Parse("12:55")-DateTime.Parse("11:45");
double hourDiff=  difference.TotalHours;
double minutes = difference.TotalMinutes;
Console.WriteLine(hourDiff);//1.16666666666667
Console.WriteLine(minutes);//70

回答by Davide Piras

create two DateTimeobjects parsing the values in the TextBoxcontrols and simply subtract the two DateTime, you will get a TimeSpanobject which is what you are looking for.

创建两个DateTime对象来解析TextBox控件中的值,然后简单地减去这两个对象DateTime,您将得到一个TimeSpan您正在寻找的对象。

回答by David Anderson

You can get a TimeSpanof the difference by subtractraction.

您可以TimeSpan通过减法获得差异。

TimeSpan time1 = TimeSpan.Parse(textBox1.Text);
TimeSpan time2 = TimeSpan.Parse(textBox2.Text);

TimeSpan difference = time1 - time2;

int hours = difference.Hours;
int minutes = difference.Minutes;

回答by Zaki

Use timespan :

使用时间跨度:

    DateTime dt1 = new DateTime(starttime.text);
    DateTime dt2 = new DateTime(endtime.text);

    TimeSpan result = dt2 - dt1;

Then you can get the minutes, seconds etc from result.

然后你可以从结果中得到分钟、秒等。

回答by goric

You can subtract two DateTimes and receive a TimeSpanstructure. This has properties for retrieving Days, Hours, Minutes, Seconds, etc.

您可以减去两个DateTimes 并接收一个TimeSpan结构。这对于检索性能DaysHoursMinutesSeconds,等。

var first = new DateTime(2012, 05, 08, 10, 30, 00);
var second = new DateTime(2012, 05, 08, 11, 49, 13);

var diff = first - second;
var hours = diff.Hours;
var mins = diff.Minutes;

回答by Binary Worrier

use TimeSpan, no need to use dates

使用 TimeSpan,无需使用日期

var start = TimeSpan.Parse(start.Text);
var end = TimeSpan.Parse(end.Text);

TimeSpan result = end - start;
var diffInMinutes = result.TotalMinutes();