两个 DateTimes C# 之间的区别?

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

Difference between two DateTimes C#?

c#datedatediff

提问by abmv

I need a function that can return the difference between the below two dates as 24.

我需要一个可以将以下两个日期之间的差值返回为 24 的函数。

DateTime a = new DateTime(2008, 01, 02, 06, 30, 00);
DateTime b = new DateTime(2008, 01, 03, 06, 30, 00);

采纳答案by Joey Robert

You can do the following:

您可以执行以下操作:

TimeSpan duration = b - a;

There's plenty of built in methods in the timespan class to do what you need, i.e.

timespan 类中有很多内置方法可以执行您需要的操作,即

duration.TotalSeconds
duration.TotalMinutes

More info can be found here.

更多信息可以在这里找到。

回答by JaredPar

Try the following

尝试以下

double hours = (b-a).TotalHours;

If you just want the hour difference excluding the difference in days you can use the following

如果您只想要不包括天数差异的小时差异,则可以使用以下内容

int hours = (b-a).Hours;

The difference between these two properties is mainly seen when the time difference is more than 1 day. The Hours property will only report the actual hour difference between the two dates. So if two dates differed by 100 years but occurred at the same time in the day, hours would return 0. But TotalHours will return the difference between in the total amount of hours that occurred between the two dates (876,000 hours in this case).

这两个属性的区别主要体现在时差大于1天的时候。小时属性将仅报告两个日期之间的实际小时差。因此,如果两个日期相差 100 年但同时发生在一天中,小时将返回 0。但 TotalHours 将返回两个日期之间发生的总小时数之间的差异(在本例中为 876,000 小时)。

The other difference is that TotalHours will return fractional hours. This may or may not be what you want. If not, Math.Round can adjust it to your liking.

另一个区别是 TotalHours 将返回小数小时。这可能是也可能不是您想要的。如果没有,Math.Round 可以根据您的喜好进行调整。

回答by Vilx-

Are you perhaps looking for:

您是否正在寻找:

int Difference = (a-b).Hours;

回答by Damien

int hours = (int)Math.Round((b - a).TotalHours)

回答by diadiora

var theDiff24 = (b-a).Hours

回答by Darshan

The time difference b/w to time will be shown use this method.

将使用此方法显示黑白与时间的时差。

 private void HoursCalculator()
    {
        var t1 = txtfromtime.Text.Trim();
        var t2 = txttotime.Text.Trim();
        var Fromtime = t1.Substring(6);
        var Totime = t2.Substring(6);
        if (Fromtime == "M")
        {
             Fromtime = t1.Substring(5);
        }
        if (Totime == "M")
        {
            Totime = t2.Substring(5);
        }

        if (Fromtime=="PM" && Totime=="AM" )
        {
            var dt1 = DateTime.Parse("1900-01-01 " + txtfromtime.Text.Trim());
            var dt2 = DateTime.Parse("1900-01-02 " + txttotime.Text.Trim());
            var t = dt1.Subtract(dt2);
            //int temp = Convert.ToInt32(t.Hours);
            //temp = temp / 2;
            lblHours.Text =t.Hours.ToString() + ":" + t.Minutes.ToString();

        }
        else if (Fromtime == "AM" && Totime == "PM")
        {
            var dt1 = DateTime.Parse("1900-01-01 " + txtfromtime.Text.Trim());
            var dt2 = DateTime.Parse("1900-01-01 " + txttotime.Text.Trim());
            TimeSpan t = (dt2.Subtract(dt1));
            lblHours.Text = t.Hours.ToString() + ":" + t.Minutes.ToString();
        }
        else
        {
            var dt1 = DateTime.Parse("1900-01-01 " + txtfromtime.Text.Trim());
            var dt2 = DateTime.Parse("1900-01-01 " + txttotime.Text.Trim());
            TimeSpan t = (dt2.Subtract(dt1));
            lblHours.Text = t.Hours.ToString() + ":" + t.Minutes.ToString();
        }
    }

use your field id's

使用您的字段 ID

var t1captures a value of 4:00AM

var t1捕获4:00AM的值

check this code may be helpful to someone.

检查此代码可能对某人有帮助。