简体   繁体   中英

DateDiff is not showing up

I've got a very simple program:

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

Trying to use DateDiff gives me the error "DateDiff is not declared. It may be inaccessible due to it's protection level." Everything I have looked at says that DateDiff should be in the Microsoft.VisualBasic namespace. I've imported that namespace as you can see. I even added a reference to it in the Solution Explorer. Very frustrating, will someone please let me know what I'm doing wrong?

You had two problems in your code

  1. DateDiff returns a long. You have a DateTime being assigned.
  2. You had a DateTime.Now-birthDate instead of a DateTime.Now, birthDate in your call
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
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

This works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM