Writing a program that will handle a limited set of expressions entered by the user of the form "Next day" isn't that hard, but if you want to handle arbitrary date query expressions, things get a little complicated. :)
But if you just want to know how to manipulate dates (and times) in Python you will have to read the documentation for the datetime and calendar modules. The datetime
module is rather large and a little bit messy, so don't expect to master it immediately. But if you read through the docs and write lots of little test programs you will soon learn how to use it.
To get you started, here's a small example which shows how to add or subtract an arbitrary number of days from a given date. To display the dates this program uses the strftime method, which you've probably already seen in the time
module docs.
#!/usr/bin/env python
import datetime
def date_string(date):
return date.strftime('%A %d %B %Y')
oneday = datetime.timedelta(days=1)
today = datetime.date.today()
print today
print 'Today is', date_string(today)
print 'Tomorrow is', date_string(today + oneday)
print 'Yesterday was', date_string(today - oneday)
print 'In one week it will be', date_string(today + oneday * 7)
output
2015-02-24
Today is Tuesday 24 February 2015
Tomorrow is Wednesday 25 February 2015
Yesterday was Monday 23 February 2015
In one week it will be Tuesday 03 March 2015