0

I need to find the difference between 2 dates in months and days in VB. Using DateDiff() function, I am not getting the desired result since DateDiff() subtracts the corresponding values only. For example:

DateDiff("m", '31/01/2012', '1/02/2012')

Returns 1 month, which is incorrect. How do I achieve this?

10
  • What will be the desired result? Do you want the result in days count only or?
    – Alex
    Jun 17, 2014 at 13:46
  • 1 Day, in case of the above example or 0 months. I want the result in the form of months and dats. For Eg: if start date is 31/01/2012 and end date is 5/3/2012, I need to have 1 month and 5 days as answer.
    – Ritwik Dey
    Jun 17, 2014 at 13:47
  • 2
    For this purpose, how many days should your "month" contain? 30, 31, 28, 29?
    – GSerg
    Jun 17, 2014 at 13:58
  • 2
    You're going to need to create a custom function to return that type of information, by converting the number of days to a month part and a day part. Your custom function will need to account for the number of days in each month (e.g., February 1 to April 3 is 63 days, and would result in 2 month, 3 days -- whereas April 1 to June 4 is 64 days but would also be 2 month, 3 days). You will also need to account for leap years... Jun 17, 2014 at 13:59
  • 1
    @RitwikDey Yes you could, but it would be more difficult (again). E.g. Month(d1) - Month(d2) only makes sense if d1 and d2 belong to the same year. DateDiff handles all that. Also see this discussion.
    – GSerg
    Jun 17, 2014 at 15:07

2 Answers 2

1

I have solved this. I had to write a function that does what I need. The algorithm is :

1) Use DateDiff to calculate the difference in months between 2 days. 2) Store the 'dd' part of both the dates in 2 different variables. 3) If 'dd' of start date is greater than 'dd' of end date, month will be Step1.value-1, else, month will be step1.value 4) Increment the Start date by the value of months we get from step 3 5) Run a loop from 1 to 31 incrementing start date by 1 until it is equal to end date. 6) value of days will be (no. of iterations)

The code is :

Dim temp As Date
Dim temp1 As Integer
Dim i As Integer
Dim day1 As Integer
Dim day2 As Integer


 temp1 = DateDiff("m", StartDate, EndDate)

 day1 = DatePart("d", StartDate)
 day2 = DatePart("d", EndDate)
 If day1 > day2 Then
    temp = DateAdd("m", (temp1 - 1), StartDate)
    Month = (temp1 - 1)
Else
    temp = DateAdd("m", (temp1), StartDate)
    Month = (temp1)
End If

For i = 1 To 31
    If temp = EndDate Then Exit For
    temp = DateAdd("d", 1, temp)
Next i

Day = (i - 1)
3
  • That for loop is not really necessary...
    – GSerg
    Jun 18, 2014 at 7:48
  • @GSerg : Could you please write an alternative for the for loop?
    – Ritwik Dey
    Jun 18, 2014 at 8:00
  • Day = DateDiff("d", temp, EndDate)
    – GSerg
    Jun 18, 2014 at 8:03
0

For Days Count Between Two Dates in VB6.0

Dim date1 As Date
Dim date2 As Date
date1 = CDate(Text1.Text)
date2 = CDate(Text2.Text)
Text3.Text = "Days between dates is: " & Format$(date2 - date1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.