Computer Learning Zone CLZ Access Excel Word Windows

Logic is the beginning of wisdom, not the end.

-Spock
 
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > Code Vault > Generic VBA > Date Time > How Long >
How Long
By Richard Rost   Richard Rost on LinkedIn Email Richard Rost   4 years ago

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

  • Post 'em below

 

Comments for How Long
 
Age Subject From
2 yearsHow LongMohiuddin Ahmed
2 yearsHowLong UpdatedRichard Rost
3 yearsHowLong Bug Fix AgainLeah Bacani
3 yearsHowLong Bug FoundRichard Rost

 

Start a NEW Conversation
 
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
 
Subscribe
Subscribe to How Long
Get notifications when this page is updated
 
 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2024 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 12/2/2024 6:01:58 AM. PLT: 0s
Keywords: code vault generic vba howlong how long until how long ago  PermaLink  How Long