VB.NET日期无效转换

时间:2014-02-09 20:50:42

标签: vb.net datetime casting

我的代码有什么问题?

    Dim iYearOld As Integer = 2010
    Dim iYearNow As Integer = 2014

    Dim iAge As Integer
    iAge = DateDiff("y", iYearOld, iYearNow)

错误

  

“无效的强制转换:参数'Date1'无法强制转换为类型日期”

有人可以帮忙吗?谢谢!

3 个答案:

答案 0 :(得分:2)

错误信息清楚地告诉你出了什么问题。

DateDiff需要2个日期参数,而你传递2个整数。

http://msdn.microsoft.com/en-us/library/b5xbyt6f(v=vs.90).aspx

答案 1 :(得分:1)

  
    

我的代码有什么问题?

  

您要将整数发送到需要日期的函数:

DateDiff

答案 2 :(得分:0)

第二个permeter,第三个是日期不是整数

Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
' Assume Sunday is specified as first day of the week. 
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)
Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2)