DateDiff没有显示

时间:2018-03-27 15:00:37

标签: vb.net datediff

我有一个非常简单的程序:

Option Strict On
Imports System
Imports Microsoft.VisualBasic

Module Program
    Sub Main(args As String())
        Dim birthDate As DateTime = DateTime.Now.AddYears(-1)
        Dim dateDifference As DateTime = DateDiff(DateInterval.Month, DateTime.Now, birthDate)
        Console.WriteLine(dateDifference.ToString)
        Console.ReadLine()
    End Sub
End Module

尝试使用DateDiff给出了错误"未声明DateDiff。由于它的保护级别,它可能无法访问。"我所看到的一切都说DateDiff应该在Microsoft.VisualBasic命名空间中。我可以看到,我已经导入了该命名空间。我甚至在解决方案资源管理器中添加了对它的引用。非常令人沮丧,有人请让我知道我做错了什么吗?

2 个答案:

答案 0 :(得分:1)

您的代码中存在两个问题

  1. DateDiff返回一个长整数。您已分配日期时间。
  2. 您的通话中有DateTime.Now-birthDate而不是DateTime.Now,birthDate
  3. Sub Main(args As String())
      Dim birthDate As DateTime = DateTime.Now.AddYears(-1)
      Dim dateDifference As Long = DateDiff(DateInterval.Month, DateTime.Now, birthDate)
      Console.WriteLine(dateDifference.ToString)
      Console.ReadLine()
    End Sub
    

答案 1 :(得分:0)

Option Strict On
Imports System
Imports Microsoft.VisualBasic

Module Program
    Sub Main(args As String())
        Dim birthDate As DateTime = DateTime.Now.AddYears(-1)
        Dim dateDifference As Long = DateDiff(DateInterval.Month, DateTime.Now, birthDate)
        Console.WriteLine(dateDifference.ToString)
        Console.ReadLine()
    End Sub
End Module

这很有效。