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

Computing a difference between dates 1

Status
Not open for further replies.

jeffcullina

Programmer
May 13, 2002
80
US
I have two dates - 5/12/02 and 5/6/03. I am looking for a routing that will return the difference in a year,month,day format. For the given dates, the function should return something like 0 years, 11 months, 25 days. The datediff function rounds so I cannot get an accurate account. When I use the month interval of datediff, I get 12 - not 11. Is there a better function?

Thanks - Jeff
 
This isn't great, but it works...

<script language=vbs>
date1 = cDate(&quot;5/12/2002&quot;)
date2 = cDate(&quot;5/6/2003&quot;)

numYears = 0
numDays = 0
numMonths = 0

if date1 < date2 then
do while date1 < date2
date1 = dateAdd(&quot;yyyy&quot;,1,date1)
numYears = numYears + 1
loop
date1 = dateAdd(&quot;yyyy&quot;,-1,date1)
numYears = numYears - 1
do while date1 < date2
date1 = dateAdd(&quot;m&quot;,1,date1)
numMonths = numMonths + 1
loop
date1 = dateAdd(&quot;m&quot;,-1,date1)
numMonths= numMonths - 1
do while date1 < date2
date1 = dateAdd(&quot;d&quot;,1,date1)
numDays = numDays + 1
loop
else
alert &quot;Date 1 must be less than date 2&quot;
end if

alert numYears & &quot; - &quot; & numMonths & &quot; - &quot; & numDays
</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top