Medjool: Ruby Date Parsing With Context

The Date.parse method in Ruby on Rails is a really useful little function. Yes, it probably comes with huge overheads, and no, obviously you wouldn’t want to be using it in performance-critical code. But every now and again it’s really ace.

Except for one thing: you can’t provide it with any context whatsoever. “Tuesday” always means next Tuesday, no matter what. “1st” always means 1st of the current month, come what may.

When building the “Quick Import” feature of, I needed a little more flexibility than that. The goal was to allow churches to copy & paste their prayer points from their weekly notice sheet, and for the site to just “know” what each date meant. So, for example, if you’ve just parsed a prayer point for “Tuesday”, odds are that when you come across a prayer point for a “Wednesday”, that almost certainly means the day after the Tuesday that you just processed. Date.parse can’t cope with that. That’s why I built Medjool – all the simplicity and flexibility of Date.parse, just with a little more context.

p ={:now => "2013-09-31".to_date})
p.parse("1st") => 1st October 2013
p.parse("3rd") => 3rd October 2013
p.parse("1st") => 1st November 2013
p.parse("Wednesday") => Wednesday 6th November 2013

I looked into doing this with Chronic, but Chronic is really geared towards Time processing rather than simply Date processing. It also seems pretty buggy when it comes to dates, and gives different results depending on the exact input date format. Medjool, by contrast, ALWAYS delegates parsing to Date.parse, it just then sometimes plays around with the result it gets back to make sure it’s after the previously parsed date, if there was any ambiguity.