469,644 Members | 1,696 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

Finding yesterday's date with datetime

Is there an easy way to determine the yesterday's date(year-month-day)
from the python datetime library if I know today's date?

Thanks
Thierry

May 15 '06 #1
2 10288
Try something like this...

HTH. A.
from datetime import *

d1 = datetime( year=2006, month=5, day=15)
d2 = datetime.now()

for d in [d1,d2]:
yest = d - timedelta(days =1 )

print "%s -> %s" % (d, yest)

May 15 '06 #2
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Thierry Lam" <la********@gmail.com> wrote:
Is there an easy way to determine the yesterday's date(year-month-day)
from the python datetime library if I know today's date?


from datetime import datetime, timedelta

today = datetime.today()
yesterday = today - timedelta(1)

# See the .month, .day, and .year fields of yesterday

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
May 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by JohnnyRuin | last post: by
15 posts views Thread by comp.lang.php | last post: by
3 posts views Thread by Jim in Arizona | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.