Monday, July 2, 2007

Python, Bash yesterday's date

I wanted to get yesterday's date in a neat way for my backup scripts (bash). I found out a simple solution for the same pretty easily.
date -d "-1 day" +%d%m will give me the yesterdays date.

date -d "-1 month" +%d%m will give me date one month previous.

Here is what you do in python for the same.
>>> import datetime
>>> today = datetime.date.today()
>>> today
datetime.date(2008, 3, 20)
>>> yesterday = today - datetime.timedelta(1)
>>> yesterday
datetime.date(2008, 3, 19)
>>> monthbefore = today - datetime.timedelta(31)
>>> monthbefore
datetime.date(2008, 2, 18)
>>>

3 comments:

lally said...

Thanks a lot!!

anon said...

Thanks this helped me

Scott Donnelly said...

Sweet, thanks!