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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Dates 1

Status
Not open for further replies.

egstatus

Programmer
Apr 14, 2005
143
0
0
US
Hi all,
I have a question regarding dates.
I have for example 01/02/2007. I want to check to see if this date is over 6 months from today's date.

if I do #01/31/2008# - #01/02/2007# I get the number of days that have passed. I want the munber of months not to be greater that 6.

Can someone help me?

Thanks

Ed
 
DateDiff("m", #1/31/2008#, #1/2/2007#)

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I just had to reverse it and it worked.

Thanks
 
That may not produce the expected results you may be looking for.

One thing to be aware of here:

The DateDiff shows in this case month intervals passed, and not months passed.

Roughly, 180 days is six months. From 31 Jan, 31 July would be 182 days.

Using DateDiff("m",#2008-1-31#,#2008-7-1#) you will get a result of 6 months, but actually it is only 152 days (5 months and 1 day).

?DateDiff("m",#1/31/2008#,#7/1/2008#) > =6
True

?DateDiff("d",#1/31/2008#,#7/1/2008#)
152

It is even more evident with this:

?DateDiff("m",#1/31/2008#,#2/1/2008#)

Even though only one day has past, the result is 1 month (interval)

I don't know if this applies to what you are looking for, but it may be relevant to it.

To adjust for this you can do something like this:
Code:
Public Function IsEndDateOver(StartDate As Date, EndDate As Date, Months As Integer) As Boolean
    IsEndDateOver = EndDate >= DateAdd("m", Months, StartDate)
End Function

?IsEndDateOver(#1/31/2008#,#7/1/2008#,6)
False

?IsEndDateOver(#1/31/2008#,#7/30/2008#,6)
False

?IsEndDateOver(#1/31/2008#,#7/31/2008#,6)
True

?IsEndDateOver(#1/31/2008#,#2/1/2008#,1)
False

?IsEndDateOver(#1/31/2008#,#2/29/2008#,1)
True

This last one produces True. One month has passed, but not 30 days. If you expect it to return Faslse, then that is again a different logic where you would probably need to always define a month as 30 days.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top