|
-
Mar 7th, 2016, 01:55 PM
#4
Re: Elegant code for TimeSince()
 Originally Posted by si_the_geek
I
Code:
lngMonths = (Year(Now) - Year(pdtmDate)) * 12 + (Month(Now) - Month(pdtmDate))
If Day(Now) < Day(pdtmDate) Then lngMonths = lngMonths -1
...but in order to get the weeks too, you need to recalculate ignoring the months:
Code:
lngWeeks = DateDiff("d", DateAdd("m", pdtmDate, lngMonths), Now) \ 7
That makes sense, thanks.
Once you have the months, the "Years, Months" calculation is along the lines of:
Code:
lngYears = lngMonths \ 12
lngMonths = lngMonths Mod 12
My function is obviously not very efficient, but I've avoided the Mod operator for years just on principle after learning on these forums that it executes (comparatively) slowly. Do you know if Mod is noticeable slower than my multiplication alternative?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|