Happy Pi day!
Explanation for those who live in countries with sensible units and date notations: Americans – aside from using weird non-metric units – also have the odd habit of putting the month first in dates, then the day, then the year. So: ‘Mar 14, 2019’ or
3/14/2019. Instead of the more sensible
On the other hand, once every year, this weird notation will spell out
3.14, or the first three digits of the circle number, π (pi). And since non-mathematicians seem to love the (non-deserved) quasi-mystical nature of Pi and mathematicians are happy that at least once a year people are paying attention, Pi-day has become kind of a thing.
It’s also an excuse to bake tasty pies as a real-world dad joke manifestation, so overall, fun can be had!
But since we are talking about date notations…
day.month.year notation common in the rest of the world is arguably better than the American
month/day/year, it is still not an ideal notation. Even better is
year-month-day, especially when used with fixed digit lengths:
While we will have to wait 1122 years before this notation approximates π, it has many other benefits. First, it sensibly and obviously sorts the components of date and time from largest (years) to lowest (seconds or even fractions of a second). It is fairly easy to parse in scripts and languages. When you sort ISO 8601 date strings alphanumerically, they will be in the correct order for dates. No other notation does that.
This is the reason this notation is enshrined as ISO 8601 and used in many places on your computer. For example, property lists encode timestamps in ISO 8601.
Learn more about property lists in my book: “Property Lists, Preferences and Profiles for Apple Administrators”
Jamf Extension Attributes can also be parsed properly as dates when returned in ISO 8601 format.
Dates in the shell
When working in shell scripts on macOS, you can use the
date command to get the current date or for other date operations. Other flavors of unix-like operating systems have a specific flag to get ISO 8601 output, but in macOS you have to format manually:
$ date -u +"%F" 2019-03-14 $ date -u +"%FT%T" 2019-03-14T08:00:15
When you want a file’s creation, access, or modification date you can use the
$ stat -f 'Created: %Sc Modified: %Sm Accessed: %Sa' pi-day Created: Mar 14 08:43:07 2019 Modified: Mar 14 08:43:06 2019 Accessed: Mar 14 08:43:06 2019
You can use the
-t option to format the time:
$ stat -f 'Created: %Sc Modified: %Sm Accessed: %Sa' -t "%FT%T" pi-day Created: 2019-03-14T08:43:07 Modified: 2019-03-14T08:43:06 Accessed: 2019-03-14T08:43:06
Finder and macOS Interface
You can teach Finder an approximation of ISO 8601. Go to System Preferences > Language & Region and click on the ‘Advanced…’ button and select the ‘Dates’ tab:
AppleScript will use the system’s date formatter (set in System Preferences) to parse and print dates. It will use the “Full date format” to show date objects by default, which is quite elaborate.
You can declare date variables with the short format, but as soon as you “compile” the script, it will be replaced by the full format. So, this:
set thedate to date "2019-03-14"
will turn into this
set thedate to date "Thursday, 14 March, 2019 at 00:00:00"
AppleScript uses the date format defined in System Preferences, so you have to have this set up. This format will also be used when AppleScript extracts a date object to a string, but again it will use the full date format by default:
get date string of thedate "Thursday, 14 March, 2019"
There is, however, a
short date string property you can use:
get short date string of thedate "2019-03-14"
Again, the output will depend on the format set in System Preferences.
- AppleScript Language Guide: date
Getting and parsing ISO dates with Python is (not surprisingly) easy.
$ python >>> import datetime >>> datetime.date.today().isoformat() '2019-03-14' >>> datetime.datetime.now().isoformat() '2019-03-14T09:03:24.107317' >>> datetime.datetime.now().replace(microsecond=0).isoformat() '2019-03-14T09:04:10'
You can also parse an ISO string with python:
>>> datetime.datetime.strptime("2019-03-14T10:09:12", "%Y-%m-%dT%H:%M:%S") datetime.datetime(2019, 3, 14, 10, 9, 12)
- Python 2.7 Documention:
Swift uses the
Date class to represent dates and times. There is a
DateFormatter object which can convert
Date objects to strings and back. There is also a dedicated, if awkwardly named,
ISO8601DateFormatter class just for ISO dates and times.
let now = Date() print(ISO8601DateFormatter().string(from: now)) let pidaywithtime = ISO8601DateFormatter().date(from: "2019-03-14T15:09:26Z")
You can even customize the behavior of the
let dateISOFormatter = ISO8601DateFormatter() dateISOFormatter.formatOptions = [ .withFullDate, .withDashSeparatorInDate] let piday = dateISOFormatter.date(from: "2019-03-14")