This function will return a string value showing how long since or how long until such as "3 months, 2 days ago."
Examples
Assuming today's date is 8/23/2020 - min and sec are advancing as I'm typing this, but you get the point
HowLong(#8/1/2018#) 2 years ago
HowLong(#8/1/2020#) 22 days ago
HowLong(#8/22/2020 10pm#) 17 hours ago
HowLong(#8/22/2020 10pm#, True) 17 hours, 30 minutes, 23 seconds ago
HowLong(#8/22/2020 10pm#, True, "hn") 17 hours, 30 minutes ago
HowLong(#8/1/2018#, True) 2 years, 22 days, 15 hours, 39 minutes, 14 seconds ago
HowLong(#8/1/2018#, True, "ymdhn") 2 years, 22 days, 15 hours, 39 minutes ago
HowLong(#8/1/2018#, True, "ymdhn", False) 2 years, 22 days, 15 hours, 39 minutes
HowLong(#6/1/2018#, True, "ym", False, True) 2 yr, 2 mo
HowLong(#1/1/2010#, False, "mmdd") 127 months ago
HowLong(#1/1/2010#, True, "mmdd") 127 months, 22 days ago
HowLong(#1/1/2025#) in 4 years
HowLong(#1/1/2025#, True, "ymdh") in 4 years, 4 months, 8 days, 8 hours
HowLong(#1/1/2021#, True, "hn", True, True) in 3128 hr, 9 min
HowLong(#8/1/2018#,,,,,#1/1/2022#) 3 years ago
HowLong(#8/1/2018#, True,,,,#1/1/2022#) 3 years, 5 months ago
HowLong(#8/1/2018#, True,"ymd",,,#1/13/2022#) 3 years, 5 months, 12 days ago
Parameters
Public Function HowLong(T As Date, Optional Exact As Boolean = False, _
Optional Accuracy As String = "ymwdhns", Optional InAgo As Boolean = True, _
Optional Abbrev As Boolean = False, Optional T2 As Date = 0) As String
- T - Date you're checking (required)
- Exact - If you want the exact date to the second
- Accuracy - a string indicating what date components you want to see (years, months, days, hours, etc.)
- InAgo - do you want to see "in 3 years" or "3 years go" instead of just "3 years"
- Abbrev - do want abbreviated date parts like "2 yr, 2 mo" instead of "2 years, 2 months"
- T2 - optional second date, so that you can determine the time difference between two dates instead of Now()
Updates
- 2021-02-22 - Fixed some bugs and added weeks (w)
- 2022-03-16 - Added T2 as an optional second date/time value. If you don't specify it, then Now() will still be used to give you the current system date/time. However, if you want to get the difference between two date/time values, you can now specify T2 and it will calculate that for you.
- And yes, I've noticed that VBA ignores your system date format. Even though I recently switched everything I have over to the ISO Date Standard Format, Access VBA doesn't seem to care. It still wants dates as mm/dd/yy. Not much I can do there.
Code
Questions