Calculating Number of Months between 2 dates

前端 未结 1 783
时光说笑
时光说笑 2021-01-14 02:01

I have the following VB.NET Code:

Dim Date1 As New DateTime(2010,5,6)
Dim Date2 As New DateTime(2009,10,12)
Dim NumOfMonths = 0 \' This is where I am stumped         


        
相关标签:
1条回答
  • 2021-01-14 02:39

    Here's a method you could use:

    Public Shared Function MonthDifference(ByVal first As DateTime, ByVal second As DateTime) As Integer
        Return Math.Abs((first.Month - second.Month) + 12 * (first.Year - second.Year))
    End Function
    

    like this:

    Dim Date1 As New DateTime(2010,5,6)
    Dim Date2 As New DateTime(2009,10,12)
    Dim NumOfMonths = MonthDifference(Date1, Date2)
    
    0 讨论(0)
提交回复
热议问题