减去2个日期时间字段以获得差距 [英] subtract 2 datetime fields to get the days left difference

查看:127
本文介绍了减去2个日期时间字段以获得差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有任何人可以帮助我找出减少2个datetime字段以获得差距的日期,请感谢。

Would appreciate it if anyone can help me figure out to substract 2 datetime fields to get the days left difference.

推荐答案

这对C#很简单。 对于比较DateTimes,我们有一个名为 TimeSpan 的类。在这种情况下,TimeSpan结构将被定义为两个数据时间之间的差异

This is very easy to do with C#. For comparing DateTimes, we have a class called TimeSpan. The TimeSpan structure, in this case, would be defined as the difference between your two datetimes.

假设您的DateTimes称为开始和结束。

Let's say that your DateTimes are called start and end.

DateTime start = new DateTime(2009, 6, 14);
DateTime end = new DateTime(2009, 12, 14);

我们已将我们的DateTimes建立到2009年6月14日和2009年12月14日。

We have established our DateTimes to June 14, 2009 and December 14, 2009.

现在,我们来看看两者之间的区别。为此,我们创建一个TimeSpan:

Now, let's find the difference between the two. To do this, we create a TimeSpan:

TimeSpan difference = end - start;

使用这个TimeSpan对象,您可以通过多种不同的方式表达差异。但是,你特别要求几天的差异,所以这里是如何得到的:

With this TimeSpan object, you can express the difference in times in many different ways. However, you specifically asked for the difference in days, so here's how you can get that:

Console.WriteLine("Difference in days: " + difference.Days);

因此,该属性称为 TimeSpan.Days

//establish DateTimes
DateTime start = new DateTime(2009, 6, 14);
DateTime end = new DateTime(2009, 12, 14);

TimeSpan difference = end - start; //create TimeSpan object

Console.WriteLine("Difference in days: " + difference.Days); //Extract days, write to Console.

有关使用TimeSpan结构的更多信息,请参阅此MSDN文档(特别是C#示例)。

For more information on using the TimeSpan structure, see this MSDN documentation (especially the C# examples).

希望我帮忙!

更新:有些答案建议您一步进行减法,例如:

UPDATE: Some answers have suggested taking doing subtraction in one step, such as with:

int days = (dt2 - dt1).Days;

int numDaysDiff = Math.Abs(date2.Subtract(date1).Days);

但是,它们与我的回答相同,只是缩短了。这是因为DateStimes的DateTime.Subtract()方法和减法运算符返回TimeSpan ,然后您可以访问这些天数。我特别在我的代码示例中使用了更长的方法,以便您清楚地了解DateTime和TimeSpan对象之间发生的事情,以及它们的工作原理。当然,我刚才提及的其他方法也很好。

However, they are the same thing as in my answer, only shortened. This is because the DateTime.Subtract() method and the subtraction operator of DateTimes returns a TimeSpan, from which you can then access the amount of days. I have specifically used the longer approach in my code sample so that you clearly understand what is going on between your DateTime and TimeSpan objects and how it all works. Of course, the other approaches I just mentioned are fine, too.

更新#2:

以前提出了一个非常类似的问题,可以在这里。然而,这个问题的要点是为什么代码示例(其实质上与所有答案相同)有时提供了一个休息日的答案。我认为这对于这个问题也是很重要的。

A very similar question was asked before, and it can be found here. However, the main point of that question was why the code sample (which is essentially equivalent to that of all the answers) sometimes provides an answer which is a day off. I think this is also important to this question.

作为其他问题的主要答案,您可以使用以下代码:

As the main answer to the other question suggests, you can use this code:

int days = (int)Math.Ceiling(difference.TotalDays);

此代码使用Math.Ceiling,根据MSDN,它是:

This code uses Math.Ceiling, which, according to MSDN, is:


返回大于或等于
指定的双精度
浮点数的最小整数值

Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.



你想如何计算这几天?



因此, strong>我们现在有一个问题,你想如何计算日子。你想计算一天中的一天(如一天中的一天),如下所示:

How Do You Want to Count the Days?

Thus, we now have an issue with how you want to count the days. Do you want to count part of a day (such as .5 of a day) as:


  • 一整天strong> - 这将使用Math.Ceiling来计算TimeSpan.TotalDays,以便您计算开始日期。

  • 某天的一部分 - 您可以只返回TimeSpan.TotalDays(不是四舍五入)作为小数(双数据类型)

  • 没有 - 你可以忽略这一天的一部分,只是返回TimeSpan.Days。

  • A full day - this would use Math.Ceiling to round up TimeSpan.TotalDays, so that you're counting started days.
  • Part of a day - you can just return the TimeSpan.TotalDays (not rounded) as a decimal (in the double datatype)
  • Nothing - you can ignore that part of a day and just return the TimeSpan.Days.

以下是上述代码示例:

以整天计数(使用Math.Ceiling()进行舍入):

Counting as a full day (using Math.Ceiling() to round up):

//establish DateTimes
DateTime start = new DateTime(2009, 6, 14);
DateTime end = new DateTime(2009, 12, 14);

TimeSpan difference = end - start; //create TimeSpan object

int days = (int)Math.Ceiling(difference.TotalDays); //Extract days, counting parts of a day as a full day (rounding up).

Console.WriteLine("Difference in days: " + days); //Write to Console.

计算为一天的一部分(不使用Math.Ceiling() ,而是以小数形式作为一天的一部分):

Counting as part of a day (NOT using Math.Ceiling(), instead leaving in decimal form as a part of a day):

//establish DateTimes
DateTime start = new DateTime(2009, 6, 14);
DateTime end = new DateTime(2009, 12, 14);

TimeSpan difference = end - start; //create TimeSpan object

double days = difference.TotalDays; //Extract days, counting parts of a day as a part of a day (leaving in decimal form).

Console.WriteLine("Difference in days: " + days); //Write to Console.

计算一天没有什么(舍入到完整数天):

Counting as nothing of a day (rounding down to the number of full days):

//establish DateTimes
DateTime start = new DateTime(2009, 6, 14);
DateTime end = new DateTime(2009, 12, 14);

TimeSpan difference = end - start; //create TimeSpan object

int days = difference.TotalDays; //Extract days, counting parts of a day as nothing (rounding down).

Console.WriteLine("Difference in days: " + days); //Write to Console.

这篇关于减去2个日期时间字段以获得差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆