DateRangeParser is a Python module which makes it easy to parse human-style date ranges like “4-8th May” or “Wed 19th June - Thurs 30th August 2014”. It is very simple, consisting of one parse method which does the parsing.
This module is released under the GNU Lesser General Public License - see the COPYING and COPYING.LESSER files in the source directory for details.
DateRangeParser can be installed from the Python Package Index by running:
pip install daterangeparser
DateRangeParser is built upon PyParsing, and requires the PyParsing module to be installed for it to work correctly. The command above should install PyParsing if it is not currently installed.
Using DateRangeParser is very easy - simply follow the steps below.
Import the parse function:
from daterangeparser import parse
Run the parse function on a string:
start, end = parse("3rd May-18th July 2014")
Use the results somehow:
print "Start = %s" % start print "End = %s" % end print "Days between start and end = %i" % (end-start).days
That’s it! Simple, isn’t it.
Most date range formats that use specific dates (rather than ‘tomorrow’ or ‘last wednesday’) should work fine. The parser works for date ranges and single dates (in this case returning a start date and None for the end date), and ignores any time details that are in the strings.
Examples that are known to work include:
More details are available in the function documentation below.
Parses a date range string and returns the start and end as datetimes.
A tuple (start, end) where each element is a datetime object. If the string only defines a single date then the tuple is (date, None). All times in the datetime objects are set to 00:00 as this function only parses dates.
This parsing routine works with date ranges and single dates, and should work with a wide variety of human-style string formats, including:
Modified so that “July” now produces a range from the 1st to the 31st July.
Added ranges based only on years, so “2013” produces a range from 1st Jan 2013 to the 31st Dec 2013, and similarly for “1995-2010”.
Added ability to parse dates with no days specified - they will default to the first or last day of the month.
“Sep 2011 - Nov 2013” will produce 01/09/2011 to 30/11/2013
This also works with single dates:
“July” will produce 01/07/XXXX (where XXXX is the current year)
Fixed minor bugs, and bumped to stable release of version 1.0
First release with main functionality.