The Blog of Ian Mercer.

Understanding Dates and Times in Natural Language

Cover Image for Understanding Dates and Times in Natural Language

One of the more challenging aspects of understanding natural language is dealing with date and time expressions. There are many different ways a user could refer to a specific date and time. They might say "Next Tuesday at 4pm", they might give a specific date in any of several different forms, they might refer to other time ranges "First Tuesday in January 2012 at 4pm" etc.

Whilst my natural language engine can't understand every possible date time expression (e.g. the second Wednesday after the first Friday in May 2010) it does handle a huge variety.

Clearly the .NET provided DateTime class is wholly inadequate to express the kinds of date/time expressions your users might enter. To deal with that I've created my own classes that represent items like a specific Time of day, a DateTimeRange, a DateTimeRangeCollection, ...

TemporalSets are the most general result of parsing a datetime expression since they can represent any date time expression. Broadly they split into two categories: finite and infinite expressions. "Tuesday at 5pm" is an infinite time expression. "Tuesday at 5pm January 2012" is a finite time expression. Sometimes you will want to accept an infinite expression and interpret it as a future or past finite occurrence. For that I have a MergePreferPast and MergePreferFuture method that operate on a TemporalSetCollection. The demonstration code on BitBucketshows this in action.

TemporalSets also have unique capabilities around both providing query expressions (for database searches) and generative expressions (for adding dates to a calendar).

If you'd like to try out the latest date / time expression parsing code in my Natural Language Engine you can visit the demo and try typing "define" followed by a date / time expression.

If you find any expressions that ought to work, please feel free to email or Tweet them to me.

Here's a sample session, you can try it out on

define june 23rd 2010 
Absolute:[DATETIMERANGE 6/23/2010 at 12:00 AM to 6/23/2010 at 11:59 PM]

define January 19th 
Future:[Thursday 1/19/2012], [Saturday 1/19/2013], [Sunday 1/19/2014], [Monday 1/19/2015], [Tuesday 1/19/2016], [Thursday
1/19/2017], [Friday 1/19/2018], [Saturday 1/19/2019], [Sunday 1/19/2020], [Tuesday 1/19/2021] Past: [Wednesday 1/19/2011] ...

Related Stories

Cover Image for Natural Language Processing

Natural Language Processing

I could not find a Natural Language Processing engine when I needed one for my home automation system so I developed my own. After 10 years of on and off development I now have a unique NLP engine for .NET that is easy to configure but incredibly powerful for precise command and control applications. It doesn't use a tokenizer so it doesn't care if you run words together.

Ian Mercer
Ian Mercer
Cover Image for World's Smartest House

World's Smartest House

Over 15 years of experimentation with home automation

Ian Mercer
Ian Mercer