DateDiff不适用于nullables

时间:2013-08-15 15:53:36

标签: vb.net

我正试图找出可以为空的日期之间的区别。 DateDiff(DateInterval.Day, firstDate, secondDate)适用于Date,但适用于Nullable(Of Date)。我的日期都是可以为空的字段。

这是错误消息:

  

重载解析失败,因为没有缩小转换就无法调用可访问的DateDiff

     
      
  • 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:参数匹配参数IntervalMicrosoft.VisualBasic.DateInterval缩小为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:参数匹配参数Date1Date?缩小为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:参数匹配参数Date2Date?缩小为Date

  •   

3 个答案:

答案 0 :(得分:2)

您必须在可空的.Value对象上调用Date

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

答案 1 :(得分:1)

如果您知道他们不是Nothing,请获取他们的值:

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

但是你可以利用operator overloading,因为这不是VB6:

(secondDate.Value - firstDate.Value).Days

答案 2 :(得分:-1)

2年后回答问题......:)

上述错误可能是由数据类型未命中匹配引起的。 使用DateValue函数,例如

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