Date Formats and Calculations in AutoHotkey

Blogs Dealing with Dates in AutoHotkey, Including Timespan Calculations, Converting Any Date Format into a Standard Timestamp (YYYYMMDD), and Other Date Tricks

Add Dates to Any Document!

 AddDate.ahk updated the MonthCal GUI control to deal with a common AutoHotkey error and combining with other scripts—included in the book A Beginner’s Guide to AutoHotkey. Two Hotstrings for instant date insertion included in the AutoHotkey Applications e-book.)

Pick from different date formats with DateMenus.ahk discussed in Chapter Thirteen, Fourteen, and Fifteen of Beginning AutoHotkey Hotstrings.

A Trick for Inserting Next Friday’s Date into Any Document,

An AutoHotkey Technique for Determining Date for Any Coming Day of the Week, Plus a Pop-up for Picking Future Weekdays

“Do You Know Next Friday’s Date?”

We record upcoming events on our monthly calendars, but we live one week at a time. Most people work Monday through Friday and relax on Saturday and Sunday. If someone gives us a date for an occasion, we ask, “What day is that?”—meaning “Give me the day of the week.” Discussed in Chapter 1.1 “Date Tricks” of Jack’s Motley Assortment of AutoHotkey Tips.

Calculating Timespans Between Dates in Years, Months, Days,

Calculating the Years, Months, and Days Between Two Points in Time Takes More Than Simple Mathematics

Years ago, I wrote an AutoHotkey timespan calculation function for keeping track of my grandkids ages. (I wrote about it in my Digging Deeper Into AutoHotkey e-book and you can find the original function in the GrandKids.ahk scripts.) Developing the function was a bit of a mindbender. As I remember, I just plowed through the project finding my way by trial-and-error. When I recently reviewed the script, I had a heck of a time figuring out what I had done. I know that I explained the steps in the book, but the script (even with the few comments) remained a mystery to me.

As I thought about it, I soon realized that I might write a better function if I changed how I viewed the problem.

Calculating Timespans in Years, Months, Days in AutoHotkey, Part 2 (Understanding the HowLong() Function),

Taking a Close Look at the HowLong() Function for Calculating Years, Months, and Days

In this blog, I discuss in its entirety the most recent AutoHotkey code for the HowLongYearsMonthsDays.ahk script (introduced in my last blog). I’ve broken it up into snippets in order to explain the purpose of each piece. To get a complete copy of the script check out HowLongYearsMonthsDays.ahk at the “ComputorEdge Free AutoHotkey Scripts” page or for a barebones version (without comments and inactive code) see “Function Calculating Timespan in Years, Months, and Days” at the AutoHotkey Forum. This blog reviews the nuts and bolts of calculating the timespan between two dates.

Using Regular Expressions to Convert Most Formatted Dates into DateTime Stamps,

AutoHotkey Offers Many Techniques for Converting the DateTime Stamp (yyyymmdd) into Formatted Dates, But What About Going in the Other Direction? Use RegEx to Identify Date Formats!

DateStamp

The HowLongYearsMonthsDays.ahk function calculates the difference in years, months, and days between any two dates. To manually set the two dates, the script employs two DateTime GUI controls—input dates saved in the DateTime Stamp format (i.e. yyyymmdd) and the output in years, months and days. But wouldn’t you find it easier if you could highlight the dates in any document or Web page regardless of format, then use AutoHotkey to convert and copy the DateTime Stamps directly into the DateTime GUI controls?

DateConvertSend
In the DateStampConvert.ahk script, a technique similar to Switch/Case statements converts the name of a month into its corresponding numeric value.

Use the Ternary Operator to Create Conditional Case Statements or Switches,

While AutoHotkey Does Not Include a Conditional Case Statement, You Can Build Your Own Using the Ternary Operator

Note, January 13, 2020: AutoHotkey now include a Switch command with more flexibility and the same speed as this pseudo-switch.

Many programming languages include Case statements which act in a manner to similar a series of If…Else If…Else statements in an abbreviated form. The simplicity of the structure provides the primary benefit of offering a series of options in semi-list form (taken from Microsoft Docs SQL example, “Using a SELECT statement with a simple CASE expression”):

USE AdventureWorks2012;  
GO  
SELECT   ProductNumber, Category =  
      CASE ProductLine  
         WHEN 'R' THEN 'Road'  
         WHEN 'M' THEN 'Mountain'  
         WHEN 'T' THEN 'Touring'  
         WHEN 'S' THEN 'Other sale items'  
         ELSE 'Not for sale'  
      END,  
   Name  
FROM Production.Product  
ORDER BY ProductNumber;  
GO

You can simulate the above Case statements in AutoHotkey using the Ternary operator:

  Value := Var = 'R' ? 'Road'
    : Var = 'M' ? 'Mountain'
    : Var = 'T' ? 'Touring'
    : Var = 'S' ? 'Other sale items'
    :'Not for sale'

Wrapping Up the DateStampConvert.ahk Script,

In Previous Blogs, I Used Regular Expressions to ID Dates Formats in Documents and Simulated Case/Switch Statements to Convert Month Names to Numbers. Now, I Build the Standard DateTime Stamp, Check for Valid Dates, and Deal with Two-digit Years, Plus Use the Function ByRef Method to Bypass Local Variables.

While in conversations with a reader who uses AutoHotkey to calculate the time span between two dates for figuring out new leases, I realized that a tool which captures formatted dates from any document and converts them into the DateTime Stamp format (yyyymmdd) would make using the HowLongYearsMonthsDays.ahk script even easier. That prompted me to write the DateStampConvert.ahk script.

A Look at the New Switch/Case Command, January 13, 2020

In the DateStampConvert.ahk Script, Rather than Using a Series of If-Else Statements (or the Ternary Operator), the New Switch Command Sets Up Case Statements for Alternative Results—Plus, Easily Add Conversions for Spanish, German, French, and Italian Date Formats

Over a year ago, I used a cascading series of the ternary operators to convert English text month names into their numeric values within a single function (“Use the Ternary Operator to Create Conditional Case Statements or Switches“). The ternary operator shortcut acts as If-Else statements in abbreviated form.

Add Dynamic Date Submenus to Static Menus Using Menu, DeleteAll,

How to Add an Updating Dynamic Submenu to a Static AutoHotkey Menu—Understanding the Difference Between Menu, DeleteAll Command and Menu, Delete Command

When I first wrote the HotstringMenu.ahk script, I planned it as a substitution for ambiguous or multiple option Hotstring text replacements. After typing one of the activation strings, a menu of alternative replacements pops-up. Since the Hotstring menus include many varied item lists, it made sense to create each only when needed. However, when I decided to combine a number of these menus into a group under one main menu, preloading the numerous static submenus seemed more practical. That left me with one problem. Some menus, such as current time and date insertion, need updating each time they pop up.

QuickLinks TimeDate Submenu
Take the three steps discussed to attach a dynamic TimeDate submenu to the QuickLinks.ahk app.

Extracting Multiple Dates from Text Using AutoHotkey RegEx, March 22, 2021

While Not Simple (and a Little Bit “Greedy”), the RegEx for Two-Date Parsing Only Requires One Selection

The inconsistency in worldwide date formats forced me to write a slightly complicated RegEx. As usual, Ryan’s RegEx Tester helped me work out the solution by giving me an environment where I could immediately see results when making even the slightest change to the key expression.

I had three objectives:

  1. Locate dates in any of the three (or more) formats identified in the DateStampConvert.ahk script (American, European, and all numeric).
  2. Extract only the first and last date in the selected text by ignoring any spurious dates appearing in-between. Only this approach would ensure the accurate selection of two target dates.
  3. Save those two dates as subpatterns for conversion into the DateTime stamp format.

Calculating Dates in AutoHotkey by Adding Years, Months, and/or Days, April 1, 2021

The HowLongYearsMonthsDays.ahk Script Calculates Time Spans—This New DateCalc() Function Yields New Dates Based on Adding or Subtracting Years, Months, or Days

I recently wrote an AutoHotkey function for calculating new dates based on entering years, months, and/or days. While not long or involved, the function includes a couple of interesting techniques.

Note: I have no doubt that many others have written similar date-calculating functions in AutoHotkey. This merely represents my stab at it.

The lack of consistency in days between 12-month years (leap years) and the varying numbers of days in months underlies the basic date calculating problem. While AutoHotkey includes a special tool (EnvAdd) for determining new dates based upon adding days, hours, minutes, or seconds to any valid date-time stamp format, writing a more complete date finding function requires accounting for the year and month variables separately.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s