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

help w/ code datediff datepart

Status
Not open for further replies.

bluesage

Programmer
Apr 27, 2004
26
CH
Private Sub Text21_Enter()

hi, here is some code i wrote, but im having some problems.
this works fine for calculating a diference in years. but im trying to CALCULATE THE NUMBER OF MONTHS between two dates IN TWO DIFFERENT YEARS

this code, returns the number of months which is correct, but it doesnt count the years and so the number of months arent right.

can anyone give me an example of how to do this?

==================================
Dim today As String
Dim arrived As String
Dim lived As String


today = DatePart("m", Date)
arrivaldate = arrivdatetxt.Value
arrived = DatePart("m", arrivaldate)

lived = DateDiff("d", arrived, today)

Text21.Value = lived

End Sub
=======================

thanks
 
Hi bluesage,

The DateDiff Function returns the difference between two Dates in the units you specify. A full explanation follows, but the answer to your question is to just use ..

Code:
[blue]Text21.Value = DateDiff("m", arrivaldate, Date)[/blue]

Now, what you have done, ..

[purple][tt] today = DatePart("m", Date)[/tt][/purple]

[tt] [/tt]This gives you the Month of today's date: [red]5[/red]

[purple][tt] arrivaldate = arrivdatetxt.Value
arrived = DatePart("m", arrivaldate)[/tt][/purple]

[tt] [/tt]This gives you the Month of the date in your field, say: [red]1[/red]

[purple][tt] lived = DateDiff("d", arrived, today)[/tt][/purple]

[tt] [/tt]This takes the two numeric Strings you have set above and converts them to Dates
[tt] [/tt]In this example the dates are 4 Jan 1900 and 31 Dec 1899
[tt] [/tt]It then finds the difference between the two dates in Days
[tt] [/tt]It is just a quirk that this happens to be the same as the number of
[tt] [/tt] year-independent months between the two original dates

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top