获取两个日期之间的日历日期

时间:2022-05-16 01:26:29

The DateDiff function reports Day Intervals on a 24 hour basis, which provides an odd result when asking how many days ago something happened when the time of day changes.

DateDiff函数以24小时为单位报告日间隔,当询问在一天中的时间发生变化时发生了多少天时,这会产生奇怪的结果。

For example, if I want to get the number of days since yesterday afternoon, if it hasn't been a full 24 hours, DateDiff will return 0, whereas most of the time I ask that question, I'm expecting yesterday to be treated as 1 day ago, regardless of timestamp.

例如,如果我想获得自昨天下午以来的天数,如果它还没有完整的24小时,DateDiff将返回0,而大多数时候我问这个问题,我期待昨天得到对待就像1天前一样,无论时间戳如何。

Is there a way to get DateDiff to return the number of calendar days?

有没有办法让DateDiff返回日历天数?

Demo in .NET Fiddle

Dim date1 As DateTime = #3/1/2017 5:00 PM#
Dim date2 As DateTime = #3/2/2017 10:00 AM#

Dim daysAgo = DateDiff(DateInterval.Day, date1, date2)
Dim msg = String.Format("Last date was {0} day(s) ago on {1}. ", daysAgo, date1.ToShortDateString())
Console.WriteLine(msg) ' Last date was 0 day(s) ago on 3/1/2017. 

The following questions on * do not address this nuance:

*上的以下问题没有解决这个细微差别:

2 个解决方案

#1


4  

You don't need string manipulation to get a DateTime without the time part, just use DateTime.Date.

您不需要字符串操作来获取没有时间部分的DateTime,只需使用DateTime.Date。

To get the time span between two dates, just subtract one from the other. Subtracting one date from another returns a TimeSpan object whose Days property is the difference in full dates.

要获得两个日期之间的时间跨度,只需从另一个日期中减去一个日期。从另一个日期中减去一个日期会返回一个TimeSpan对象,其Days属性是完整日期的差异。

All you need to do is :

您需要做的就是:

Dim days As Integer =(date2.Date-date1.Date).Days 

This way you avoid generating and parsing temporary strings.

这样就可以避免生成和解析临时字符串。

#2


0  

You can use DateTime.Date to remove the time component and compare the whole number dates like this:

您可以使用DateTime.Date删除时间组件并比较整数日期,如下所示:

Demo in .NET Fiddle

Dim date1 As DateTime = #3/1/2017 5:00 PM#
Dim date2 As DateTime = #3/2/2017 10:00 AM#

Dim daysAgo = DateDiff(DateInterval.Day, date1.Date, date2.Date)
Dim msg = String.Format("Last date was {0} day(s) ago on {1}. ", daysAgo, date1.ToShortDateString())
Console.WriteLine(msg) ' Last date was 1 day(s) ago on 3/1/2017. 

#1


4  

You don't need string manipulation to get a DateTime without the time part, just use DateTime.Date.

您不需要字符串操作来获取没有时间部分的DateTime,只需使用DateTime.Date。

To get the time span between two dates, just subtract one from the other. Subtracting one date from another returns a TimeSpan object whose Days property is the difference in full dates.

要获得两个日期之间的时间跨度,只需从另一个日期中减去一个日期。从另一个日期中减去一个日期会返回一个TimeSpan对象,其Days属性是完整日期的差异。

All you need to do is :

您需要做的就是:

Dim days As Integer =(date2.Date-date1.Date).Days 

This way you avoid generating and parsing temporary strings.

这样就可以避免生成和解析临时字符串。

#2


0  

You can use DateTime.Date to remove the time component and compare the whole number dates like this:

您可以使用DateTime.Date删除时间组件并比较整数日期,如下所示:

Demo in .NET Fiddle

Dim date1 As DateTime = #3/1/2017 5:00 PM#
Dim date2 As DateTime = #3/2/2017 10:00 AM#

Dim daysAgo = DateDiff(DateInterval.Day, date1.Date, date2.Date)
Dim msg = String.Format("Last date was {0} day(s) ago on {1}. ", daysAgo, date1.ToShortDateString())
Console.WriteLine(msg) ' Last date was 1 day(s) ago on 3/1/2017.