Difference between two dates (1 Viewer)

rvsebi

Registered User.
Local time
Today, 15:08
Joined
Jun 1, 2015
Messages
77
Hi, i made a form where i calculate difference between dates.
Code:
..............
Dim tyears As Integer, tmonths As Integer, tdays As Integer
tyears = Year(Me!txtdata1) - Year(Me!txtdata2)
tmonths = Month(Me!txtdata1) - Month(Me!txtdata2)
tdays = Day(Me!txtdata1) - Day(Me!txtdata2)
If tmonths < 0 Then
    tyears = tyears - 1
    tmonths = tmonths + 12
End If
If tyears < 0 Then tyears = 0
If tdays < 0 Then tmonths = tmonths - 1
MsgBox (tyears & "years" & tmonths & "months" & tdays & "days")
..................

Sometimes days are negative numbers. When days are negative numbers i need to make days + 30 or days +31?
I mean this : " If tdays < 0 then tdays = tdays +30" or " If tdays < 0 then tdays = tdays +31" ?
Thank you!
 

Isaac

Lifelong Learner
Local time
Today, 06:08
Joined
Mar 14, 2017
Messages
8,738
Sometimes dates are negative numbers?
 

rvsebi

Registered User.
Local time
Today, 15:08
Joined
Jun 1, 2015
Messages
77
Sometimes dates are negative numbers?

Sometimes "days" are negative numbers. It is days, just read again.
 
Last edited:

bob fitz

AWF VIP
Local time
Today, 13:08
Joined
May 23, 2011
Messages
4,717
Hi, i made a form where i calculate difference between dates.
Code:
..............
Dim tyears As Integer, tmonths As Integer, tdays As Integer
tyears = Year(Me!txtdata1) - Year(Me!txtdata2)
tmonths = Month(Me!txtdata1) - Month(Me!txtdata2)
tdays = Day(Me!txtdata1) - Day(Me!txtdata2)
If tmonths < 0 Then
    tyears = tyears - 1
    tmonths = tmonths + 12
End If
If tyears < 0 Then tyears = 0
If tdays < 0 Then tmonths = tmonths - 1
MsgBox (tyears & "years" & tmonths & "months" & tdays & "days")
..................

Sometimes days are negative numbers. When days are negative numbers i need to make days + 30 or days +31?
I mean this : " If tdays < 0 then tdays = tdays +30" or " If tdays < 0 then tdays = tdays +31" ?
Thank you!
Perhaps the following code would be of use. I take NO credit for the code. It's just one of many useful functions from the attached db which I believe was given by Pat Hartman.
Code:
Function CalculateLongAge(DOB As Date, CurDate As Date) As String
    Dim Y As Integer
    Dim M As Integer
    Dim D As Integer
    Dim Temp1 As Date
    Temp1 = DateSerial(Year(CurDate), month(DOB), Day(DOB))
    Y = Year(CurDate) - Year(DOB) + (Temp1 > CurDate)
    M = month(CurDate) - month(DOB) - (12 * (Temp1 > CurDate))
    D = Day(CurDate) - Day(DOB)
    If D < 0 Then
        M = M - 1
        D = Day(DateSerial(Year(CurDate), month(CurDate) + 1, 0)) + D + 1
    End If
    CalculateLongAge = Y & " years " & M & " months " & D & " days"
End Function
 

Attachments

  • UsefulDateFunctions.zip
    189.1 KB · Views: 250

Minty

AWF VIP
Local time
Today, 13:08
Joined
Jul 26, 2013
Messages
10,354
Bizarre - two threads almost identical on the same day - have a look here :

 

plog

Banishment Pending
Local time
Today, 08:08
Joined
May 11, 2011
Messages
11,611
Sometimes days are negative numbers. When days are negative numbers i need to make days + 30 or days +31?

In short--yes. As I said in the other thread, when you want to work in months, days and years the best you can hope for is to get it to kind of work. So add 30 days, or 31, or 29 and sometimes add 28. Months, years and days do not share a common base unit, you cannot get this to work as if it does.

For the specific issue of negative numbers I suggest you test to see which date is greater (txtdata2>txtdata1) and swap them to make sure there is never a negative number.
 

Users who are viewing this thread

Top Bottom