How to test if a date comes earlier or later than another date

Censuses were taken in the UK every 10 years from 1841. I have set up a variable (UKCensuses) that contains the list of publication dates (6 Jun 1841, 30 Mar 1851....).
For a person born in 1877 and who died in 1942 I want to identify which censuses he/she may have appeared in, i.e., the logic is to examine each line in UKCensuses and compare that date to the birth/death dates.

I have read/reread all the wiki and forum discussions about ICUdate tokens etc. but as a I am not a computer expert and I'm just learning KM, I find them bemusing. I get the concept of dates stored as numbers and I've tried 'Calculate(%localCensusDate% > 1877)' and all the variations I can think of to no avail.

So I'm hoping to get a nudge in the right direction please. How do I test if one date comes before/after another... or perhaps how to I convert '6 Jun 1841' and '1877' to forms that KM can do the comparison ?

The simplest comparable date string format is the ISO 8601 yyyy-mm-dd pattern, which can be sorted and compared because the larger values are to the left, and the month values are numeric.

If you are happy to enter dates in that format things may get simpler.
Otherwise, KM can convert date string formats for you.

Since you only want to compare dates and not find differences, the quickest way is to convert them to yyyyMMdd and do a string comparison -- if you want to make them easier on the eye, add delimiters: yyyy-MM-dd. So "30 Mar 1851" becomes 18510330 or 1851-03-30.

The problem with that is converting month names to numbers -- I can't see an easy way to do that in KM. So, given that your stated date format is "day month year", I'd turn to AppleScript because it can coerce a string in that format to a date with no other work from us:


Date Compare.kmmacros (2.5 KB)

Thank you! In fact the answer was even simpler than you suggested... as often is the case I was overcomplicating things. In fact for my purpose here only the year is of importance so I can ignore dates altogether and simply us '1881 < 1900' and it works.

1 Like

thanks Nice-S. I'll keep this in mind but for now learning KM is enough for me to handle... Apple script will have to wait a wile :grinning: