Calculating Years, Months, Days between two dates (1 Viewer)

Mike1234

New member
Local time
Yesterday, 18:41
Joined
Nov 20, 2017
Messages
4
Hi can anyone please help!!

I am using the below code to calculate the Years,Months,Days between two dates, but I want to use the current date if my End Date is empty..
How would I do this?


Public Function Calcdays(StartDate As Date, EndDate As Date) As String


Dim intYears As Integer, intMonths As Integer, intDays As Integer
intMonths = DateDiff("m", StartDate, EndDate)
intDays = DateDiff("d", DateAdd("m", intMonths, StartDate), EndDate)
If intDays < 0 Then
intMonths = intMonths - 1
intDays = DateDiff("d", DateAdd("m", intMonths, StartDate), EndDate)
End If
intYears = intMonths \ 12
intMonths = intMonths Mod 12
Calcdays = " " & intYears & " Years, " & intMonths & " Months, " & intDays & " Days"

End Function

My result is =Calcdays([StartDate],[EndDate])
 

plog

Banishment Pending
Local time
Yesterday, 20:41
Joined
May 11, 2011
Messages
11,638
Public Function Calcdays(StartDate As Date, Optional EndDate As Date = Date()) As String
 

Mike1234

New member
Local time
Yesterday, 18:41
Joined
Nov 20, 2017
Messages
4
Public Function Calcdays(StartDate As Date, Optional EndDate As Date = Date()) As String

I have tried this, but the() after =Date()) keeps disappearing?
I dont know why
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 20:41
Joined
Feb 28, 2001
Messages
27,131
VBA's autocorrect is kicking in, that's why. Question is, does it still work? If so, then VBA's "language-sensitive environment" editor is just showing off.
 

isladogs

MVP / VIP
Local time
Today, 02:41
Joined
Jan 14, 2017
Messages
18,209
The brackets shouldn't be there - a very rare event, but on this occasion, plog wrote it incorrectly. It should be:

Code:
Public Function Calcdays(StartDate As Date, Optional EndDate As Date) As String
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:41
Joined
May 7, 2009
Messages
19,228
Function Calcdays(StartDate As Date, Optional EndDate As Variant=0) As String
If IsNull(EndDate) Then EndDate=Date
If EndDate=0Then EndDate=Date
 
Last edited:

Mike1234

New member
Local time
Yesterday, 18:41
Joined
Nov 20, 2017
Messages
4
Function Calcdays(StartDate As Date, Optional EndDate As Variant=0) As String
If IsNull(EndDate) Then EndDate=Date
If EndDate=0Then EndDate=Date

This worked exactly as needed
Thanks
Keep up the good work ALL
 

Habib Zeb

New member
Local time
Today, 06:41
Joined
Apr 5, 2023
Messages
1
Can anyone Help me?

I have used this function to calculate between two dates but when the the date is empty it shows #Type!
anyone please help me if the date is empty then it will also empty.


[/CODE]Option Compare Database

Function DateDiffExact(startDate As Date, endDate As Date) As String
Dim years As Long, months As Long, days As Long
Dim startY As Long, startM As Long, startD As Long
Dim endY As Long, endM As Long, endD As Long
startY = Year(startDate)
startM = Month(startDate)
startD = Day(startDate)
endY = Year(endDate)
endM = Month(endDate)
endD = Day(endDate)
years = endY - startY
months = endM - startM
days = endD - startD
If days < 0 Then
days = days + Day(DateSerial(endY, endM + 1, 0))
months = months - 1
End If
If months < 0 Then
months = months + 12
years = years - 1
End If
DateDiffExact = years & " y | " & months & " m | " & days & " d "

End Function
[/CODE]
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 18:41
Joined
Oct 29, 2018
Messages
21,449
Hi @Habib Zeb

Welcome to AWF!

This is a six-year old thread. You might consider starting a new one instead.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:41
Joined
May 7, 2009
Messages
19,228
declare your parameters as Variant:
Code:
Function DateDiffExact(ByVal startDate As Variant, ByVal endDate As Variant) As String
    Dim years As Long, months As Long, days As Long
    Dim startY As Long, startM As Long, startD As Long
    Dim endY As Long, endM As Long, endD As Long
    
    'arnelgp
    startDate = Nz(startDate, Date)
    endDate = Nz(endDate, Date)
    
    startY = Year(startDate)
    startM = Month(startDate)
    startD = Day(startDate)
    endY = Year(endDate)
    endM = Month(endDate)
    endD = Day(endDate)
    years = endY - startY
    months = endM - startM
    days = endD - startD
    
    If days < 0 Then
        days = days + Day(DateSerial(endY, endM + 1, 0))
        months = months - 1
    End If
    If months < 0 Then
        months = months + 12
        years = years - 1
    End If
    DateDiffExact = years & " y | " & months & " m | " & days & " d "

End Function
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 11:41
Joined
Jan 20, 2009
Messages
12,851
declare your parameters as Variant:
It is generally a better practice to declare the parameter datatype and validate the arguments are present before passing them to the function.

Arnelg's function would error internally if passed a string argument.

Microsoft doesn't use Variants for function arguments when a specific datatype is expected.

Variants also use a lot more resources because they can also accept objects.
 

Users who are viewing this thread

Top Bottom