user2686497
user2686497

Reputation: 13

DateDiff doesn’t work with nullables

I’m trying to find the difference between nullable dates. DateDiff(DateInterval.Day, firstDate, secondDate) works for Date, but not for Nullable(Of Date). Both my dates are nullable fields.

This is the error message:

Overload resolution failed because no accessible DateDiff can be called without a narrowing conversion:

  • Public Function DateDiff(Interval As String, Date1 As Object, Date2 As Object, [DayOfWeek As Microsoft.VisualBasic.FirstDayOfWeek = FirstDayOfWeek.Sunday], [WeekOfYear As Microsoft.VisualBasic.FirstWeekOfYear = FirstWeekOfYear.Jan1]) As Long: Argument matching parameter Interval narrows from Microsoft.VisualBasic.DateInterval to String.

  • Public Function DateDiff(Interval As Microsoft.VisualBasic.DateInterval, Date1 As Date, Date2 As Date, [DayOfWeek As Microsoft.VisualBasic.FirstDayOfWeek = FirstDayOfWeek.Sunday], [WeekOfYear As Microsoft.VisualBasic.FirstWeekOfYear = FirstWeekOfYear.Jan1]) As Long: Argument matching parameter Date1 narrows from Date? to Date.

  • Public Function DateDiff(Interval As Microsoft.VisualBasic.DateInterval, Date1 As Date, Date2 As Date, [DayOfWeek As Microsoft.VisualBasic.FirstDayOfWeek = FirstDayOfWeek.Sunday], [WeekOfYear As Microsoft.VisualBasic.FirstWeekOfYear = FirstWeekOfYear.Jan1]) As Long: Argument matching parameter Date2 narrows from Date? to Date.

Upvotes: 1

Views: 2117

Answers (3)

Kuuri Eliud
Kuuri Eliud

Reputation: 1

2 Years Late to Answer the question.... :)

The error Above could be caused by a data type miss match. Use DateValue function e.g

DateDiff(DateInterval.Day, DateValue(stringwiDate.text), DateValue(string2witDate.text))

Upvotes: -1

Ry-
Ry-

Reputation: 224942

If you know they’re not Nothing, get their values:

DateDiff(DateInterval.Day, firstDate.Value, secondDate.Value)

But you can take advantage of operator overloading, since this isn’t VB6:

(secondDate.Value - firstDate.Value).Days

Upvotes: 1

OneFineDay
OneFineDay

Reputation: 9024

You have to call the .Value on the nullable Date object.

DateDiff(DateInterval.Day, firstDate.Value, secondDate.Value) 

Upvotes: 2

Related Questions