Yesterday’s date in Python

To subtract a day in Python (to get yesterday’s date), here is one way of doing it:

import time

now = time.time()
today = time.localtime(now)
yesterday = time.localtime(now - 60*60*24)
  • Share/Bookmark

2 Comments

TrautDecember 12th, 2007 at 4:39 pm

thank you :)

Wyatt BaldwinJanuary 7th, 2009 at 2:18 am

How about this:

  1. assuming you don’t need hours, minutes, and seconds:

import datetime
today = datetime.date.today()
yesterday = today – datetime.timedelta(1)

  1. or, if you do need hours, minutes, and seconds:

now = datetime.datetime.now()
yesterday_at_this_time = now – datetime.timedelta(1)

Leave a comment

Your comment