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.
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.
Is there a way to get DateDiff to return the number of calendar days?
Demo in .NET Fiddle
<!-- language: lang-vb -->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 StackOverflow do not address this nuance: