Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function for determing number of days between 2 dates? 1

Status
Not open for further replies.

SSJpn

Technical User
Oct 7, 2002
259
US
I was wondering if there was a build in function that could compare 2 dates and determine the number of days between them.

Otherwise would anyone be willing to post some code that accomplishes this? It would be ok to assume that all months have 31 days. I would like it if the parameters were as follows but they can be changed if it makes it any easier

ByVal Month1 as integer
ByVal Day1 as integer
ByVal Year1 as integer
ByVal Month2 as integer
ByVal Day2 as integer
ByVal Year2 as integer

The function would return an integer value representing the number of dayes between the two dates.

Assume already that Month1 Day1, Year1 comes first
 
The non-VB approach would be to simply subtract the later date from the earlier and then format that cell as a number. Mike

Before: After
[morning] [bugeyed]
 
=DATE(F2,D2,E2)-DATE(C2,A2,B2)

assuming the following set-up:

A1: 'Month1
B1: 'Day1
C1: 'Year1
D1: 'Month2
E1: 'Day2
F1: 'Year2
G1: 'Days Diff
A2: 6
B2: 15
C2: 2002
D2: 7
E2: 2
F2: 2002
G2: =DATE(F2,D2,E2)-DATE(C2,A2,B2)

G2 has the value 17

 
If you have two dates, you can use the VBA functiom DateDiff,

DateDiff(interval, startdate,enddate)

where the interval represents the intervals you want to measure such as days, months, weeks, or even hours or minutes.

To get the number of days between 2 dates use

DateDiff("d", StartDate, EndDate).

Check out the VBA help for more detail.

A.C.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top