Past and Future Dates with TextExpander, Revamped

Easy dates

We regularly receive requests for a snippet group which handles past and future dates, such as: last Wednesday or four days from now. We typically respond by sending folks the DatesUS group or pointing folks to Thought Asylum’s Dates group. Both are good, but neither take full advantage of TextExpander’s features.

I set out to improve these groups with the following goals:

  1. Must be drop-in compatible with the old DatesUS group
  2. Must be cross-platform JavaScript so that it runs on macOS, Windows, and iOS
  3. Must support your system locale as the default, so we’re no longer US or UK centric
  4. Must support setting a specific locale if you wish
  5. Must use a group prefix which can be overridden if you wish
  6. Should allow you to specify the date format details

I’m pleased to report that I’ve met all of my goals.

Past and Future Dates

To use the new Past and Future Dates group, you can find it on our Public Groups page, or click the above link. If you’re not signed in to Textexpander.com, you’ll need to do that before you can access the group.

If all you’re doing is replacing DatesUS, you can stop here. If you’d like to roll up your sleeves and customize things, read on.

Setting the Locale

By default, Past and Future Dates uses your system locale. But let’s say you run your system in English and live in Germany. You’d probably prefer that your dates appear in German style. To specify your locale:

  1. Create a new snippet
  2. Set the abbreviation to:
'locale_for_Dates

(including the leading apostrophe)
3. Set the content to the locale you wish to use, which for German is: de

Don’t include a new line after your locale, or you’ll wind up with “JavaScript Exception: SyntaxError: Unexpected identifier ‘Exception'” as the result of every Past and Future Dates snippet.

Here are some other fun locales you can try (the part before the colon):

ar-EG : Arabic, as used in Egypt
en-GB : English, as used in Great Britain (thus: day/month/year)
fr-CA : French, as used in Canada
zh : Chinese, non-specific

Here is the official documentation for BCP 47 language tags. It’s super technical. You may find you’re better off Googling for “language tag” and your specific language situation.

Setting the Format

By default, Past and Future Dates uses 4-digit years and 2-digit months and days with leading zeros. If you prefer otherwise, you can specify the exact date options to use. Let’s say you don’t want leading zeros:

  1. Create a new snippet
  2. Set the abbreviation to:
'options_for_Dates

(including the leading apostrophe)
3. Set the content to the date options you wish to use, in this case:

{ year: 'numeric', month: 'numeric', day: 'numeric' }

If you’d prefer the weekday and month name, try:

{ weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }

Which expands to: Monday, November 27, 2017

If you’re using the Chinese locale, you’ll probably prefer:

{ year: 'numeric', month: 'long', day: 'numeric' }

That way, you’ll get dates like this: 2017年11月27日

The default value is:

{ month: '2-digit', day: '2-digit', year: 'numeric'}

Don’t include a new line after your options, or you’ll wind up with “JavaScript Exception: SyntaxError: Unexpected identifier ‘Exception'” as the result of every Past and Future Dates snippet.

Search for “Formatting Options” on this page for details on how the options settings work, including an interactive form you can play with.

Group Prefix

By default, the abbreviations have a prefix of apostrophe ( ‘ ). If you’d prefer otherwise, click on the group to get to the Group Settings, check “Override”, and enter the prefix you prefer. On iOS, you may prefer to use a double letter prefix, such as: xx. Read this article to learn more about abbreviation prefixes.

Thanks, Stephen!

Special thanks to Stephen Millard (@sylumer) of Thought Asylum for granting us permission to create and distribute derivative works from some of his existing code. Much obliged.