Function Repository Resource:

DateAmbiguityBreak

Source Notebook

Interpret date strings by specifying how ambiguity should be handled

Contributed by: Sjoerd Smit

ResourceFunction["DateAmbiguityBreak"]["DayFirst"][string]

interprets string as a DateObject, using a day-before-month interpretation if there is ambiguity.

ResourceFunction["DateAmbiguityBreak"]["MonthFirst"][string]

interprets string as a DateObject, using a month-before-day interpretation if there is ambiguity.

ResourceFunction["DateAmbiguityBreak"][{elem1,elem2,elem3}][string]

specifies a custom day-month-year pattern for prioritization.

ResourceFunction["DateAmbiguityBreak"][Function[]][string]

applies a function to an Association holding all possible interpretations and ambiguity data.

ResourceFunction["DateAmbiguityBreak"][Interpreter[], pattern][string]

uses the specified Interpreter to interpret string.

Details

ResourceFunction["DateAmbiguityBreak"] is intended as an extension of the default auto-magic Wolfram date interpretation functionality that will just "do the right thing" for nearly every input, since the month-first or day-first ambiguity is usually the only source of ambiguity encountered in practice.
ResourceFunction["DateAmbiguityBreak"][pattern] uses Interpreter["Date"|"DateTime"] to interpret the input.
Dates like "2022-01-02" will always be assumed to be in year-month-day form.
Any date that's completely unambiguous (like "15-08-2022") will always be interpreted irrespective of the pattern specified.
If the year specification is ambiguous because it's given by only two digits, the year-last interpretation is used since the ISO standard mandates four year digits and does not accommodate the yy-mm-dd format.
ResourceFunction["DateAmbiguityBreak"] threads over lists of dates.

Examples

Basic Examples (2) 

Interpret an ambiguous date string:

In[1]:=
ResourceFunction["DateAmbiguityBreak"]["MonthFirst"]["08-03-2022"]
Out[1]=

Interpret ambiguous strings with date and time information:

In[2]:=
ResourceFunction["DateAmbiguityBreak"][
  "DayFirst"]["08-03-2022T14:30:23"]
Out[2]=
In[3]:=
ResourceFunction["DateAmbiguityBreak"][
  "MonthFirst"]["08-03-2022T14:30:23"]
Out[3]=

Strings in year-first form are always assumed to be in the ISO standard year-month-day form:

In[4]:=
ResourceFunction["DateAmbiguityBreak"]["DayFirst"]["2022-03-08"]
Out[4]=
In[5]:=
ResourceFunction["DateAmbiguityBreak"]["MonthFirst"]["2022-03-08"]
Out[5]=
In[6]:=
% === %%
Out[6]=

Scope (4) 

Interpret multiple strings at a time:

In[7]:=
ResourceFunction["DateAmbiguityBreak"]["DayFirst"][{"08-03-2022", "08-03-2022T14:30:23"}]
Out[7]=

Specify your own preferred formats to break ambiguity:

In[8]:=
ResourceFunction[
  "DateAmbiguityBreak"][{"Year", "Day", "Month"} | {"Month", "Day", "Year"}][
 {"2022-03-08", "08-03-2022"}
 ]
Out[8]=

Specify a custom function to apply to the ambiguity data in the form of an Association to make it easier to pick compatible interpretations:

In[9]:=
ResourceFunction["DateAmbiguityBreak"][Function[#]]["08-03-20"]
Out[9]=
In[10]:=
ResourceFunction["DateAmbiguityBreak"][
  Function[
   Select[#, Last[#Value] === "Year" &]
   ]
  ]["08-03-20"]
Out[10]=

Use a more restrictive Interpreter to parse only pure date strings:

In[11]:=
ResourceFunction["DateAmbiguityBreak"][Interpreter["Date"], "DayFirst"][{"08-03-2022", "08-03-2022T14:30:23"}]
Out[11]=

Properties and Relations (2) 

DateAmbiguityBreak uses an AmbiguityFunction to find the desired interpretation from an AmbiguityList:

In[12]:=
Interpreter["Date", AmbiguityFunction -> All][{"08-03-2022", "15-03-2022"}]
Out[12]=

Note that unambiguous strings do not get placed in an AmbiguityList.


When the year is given by only two digits, a year-last interpretation is always preferred since the ISO standard mandates four year digits. Thus, dd-mm-yy takes precedence over yy-mm-dd:

In[13]:=
ResourceFunction["DateAmbiguityBreak"]["DayFirst"]["01-02-03"]
Out[13]=

mm-dd-yy takes precedence over yy-mm-dd:

In[14]:=
ResourceFunction["DateAmbiguityBreak"]["MonthFirst"]["01-02-03"]
Out[14]=

Possible Issues (1) 

Any date string that's completely unambiguous will be interpreted, even if it's incompatible with the specified pattern:

In[15]:=
ResourceFunction["DateAmbiguityBreak"]["MonthFirst"]["15-08-2022"]
Out[15]=

Publisher

Sjoerd Smit

Version History

  • 1.0.0 – 15 August 2022

Related Resources

License Information