Calculating number of Months between 2 dates

• Question

• how to calculate number of Months between 2 dates from mysql?

• Edited by Monday, February 11, 2019 8:11 PM
Monday, February 11, 2019 8:00 PM

• Hi,

try the code:

```Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim currentDate As DateTime = DateTimePicker1.Value
Dim dynamicTime As DateTime = DateTimePicker2.Value
Dim year As Integer = currentDate.Year - dynamicTime.Year
Dim month As Integer = (currentDate.Year - dynamicTime.Year) * 12 + (currentDate.Month - dynamicTime.Month)
Dim month1 As Integer = currentDate.Year * 12 + currentDate.Month - dynamicTime.Year * 12 - dynamicTime.Month
Dim used As TimeSpan = DateTime.Now - dynamicTime
Dim totalDays As Double = used.TotalDays
TextBox1.Text = Math.Abs(Convert.ToInt32(month)).ToString
End Sub
End Class```

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Marked as answer by Monday, February 18, 2019 1:57 PM
Tuesday, February 12, 2019 3:03 AM

All replies

• Hi

Here is an example of number of months calculation - see if you can MYSQL' ize it!

```    ' January 2018
Dim d1 As Date = Now.AddMonths(-13)
' April 2019
Dim d2 As Date = Now.AddMonths(2)

' 15 months
Dim d3 As Long = DateDiff(DateInterval.Month, d1, d2)```

Regards Les, Livingston, Scotland

• Edited by Tuesday, February 12, 2019 1:01 PM Corrected Integer to Long - Thanks IronRazerz
Monday, February 11, 2019 8:21 PM
• Hello,

Here is a method

```Dim date1 As Date = #7/23/2019#
Dim date2 As Date = #5/23/2019#

Dim result = ((date1.Year - date2.Year) * 12) + date1.Month - date2.Month
Console.WriteLine(result)```

Result = 2

While this will be -2

```Dim date1 As Date = #5/23/2019#
Dim date2 As Date = #7/23/2019#

Dim result = ((date1.Year - date2.Year) * 12) + date1.Month - date2.Month
Console.WriteLine(result)```

Or always positive

```Dim date1 As Date = #5/23/2019#
Dim date2 As Date = #7/23/2019#

Dim result = ((date1.Year - date2.Year) * 12) + date1.Month - date2.Month

If result < 0 Then
Console.WriteLine(result * -1)
Else
Console.WriteLine(result)
End If```

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

Monday, February 11, 2019 8:24 PM
• thank you for you answer but what if my date is from datetimepicker?
Monday, February 11, 2019 9:10 PM
• thank you for you answer but what if my date is from datetimepicker?

A DateTimePicker.Value is a Date type.  All you need to do is replace the Date types in the examples with the DateTimePicker1.Value and DateTimePicker2.Value.

EDIT:

This is the same method that leshay has shown but,  using 2 DateTimePicker controls and a correction to the type conversion error.   The DateDiff method returns a Long type,  not an Integer type.  8)

```Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim date1 As Date = DateTimePicker1.Value
Dim date2 As Date = DateTimePicker2.Value
Dim months As Long = Math.Abs(DateDiff(DateInterval.Month, date1, date2))
Me.Text = months.ToString & " months"
End Sub
End Class
```

If you say it can`t be done then i`ll try it

• Edited by Monday, February 11, 2019 10:30 PM
Monday, February 11, 2019 9:23 PM
• Hi,

try the code:

```Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim currentDate As DateTime = DateTimePicker1.Value
Dim dynamicTime As DateTime = DateTimePicker2.Value
Dim year As Integer = currentDate.Year - dynamicTime.Year
Dim month As Integer = (currentDate.Year - dynamicTime.Year) * 12 + (currentDate.Month - dynamicTime.Month)
Dim month1 As Integer = currentDate.Year * 12 + currentDate.Month - dynamicTime.Year * 12 - dynamicTime.Month
Dim used As TimeSpan = DateTime.Now - dynamicTime
Dim totalDays As Double = used.TotalDays
TextBox1.Text = Math.Abs(Convert.ToInt32(month)).ToString
End Sub
End Class```

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Marked as answer by Monday, February 18, 2019 1:57 PM
Tuesday, February 12, 2019 3:03 AM