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

Convert Days to Years/Months

Status
Not open for further replies.

cglinn

Programmer
Sep 3, 2003
44
US
I have a query that returns a list of 2 dates, like so:

Date1 Date2
11/01/1999 04/14/2004
01/01/2003 09/23/2004
.......... ...........

What I need to do is get an average time elapsed between the two dates for the entire recordset.

So far, I loop through each row and get the difference (in days) between the two dates using the datediff() function. I add the difference to a running total to get a total number of days and then divide by the number of records to get an average number of days.

My problem is that I need to turn the average number of days into Years, Months, Days.

Any Suggestions?
 
How about something along these lines?

lNoOfDays = DaysReturned

iNoOfYears = Cint(lNoOfDays /352)

INoOfMonths = Cint(lNoOfDays / 30)

Everybody body is somebodys Nutter.
 
Hello cglinn,
Code:
dtDate1=#11/01/1999#
dtDate2=#04/14/2004#
dtDeltaD=datediff("d",dtDate1,dtDate2)
dtDeltaM=datediff("m",dtDate1,dtDate2)
dtDeltaY=datediff("yyyy",dtDate1,dtDate2)
wscript.echo dtDeltaD & vbcrlf &  dtDeltaM & vbcrlf & dtDeltaY
Summation over Loop and take average. There is highly conventional notion of year and month. In financial market you might be asked to use year=360 days... So if every expression of the average comes from days, you have to ask for the definition.

regards - tsuji
 
tsuji,

Although this isn't really the solution I had originally wanted, it is also the one I had come up with. What I was really hoping for was a combination of the three for a single value. For example,

3 Years, 2 Months, and 14 Days.

Thank you all for your suggestions.



 
Hello again,

For 30-day month, 360-day year, you can use something like this to do it.
Code:
df=datediff("d",dtDate1,dtDate2)
df_inwords=cstr(df\360) & " year(s) " & cstr((df mod 360)\30) & " month(s) " & cstr(df mod 30) & " day(s)"
wscript.echo df_inwords
regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top