[Python] How to get yesterday's date

tata-titi Posted messages 43 Status Membre -  
sebsauvage Posted messages 33284 Registration date   Status Modérateur Last intervention   -
Is there an existing function or "conversion" that would allow for getting yesterday's date with ...time...???

Thank you
Configuration: Windows XP Firefox 1.5.0.9

4 réponses

teebo Posted messages 33570 Registration date   Status Modérateur Last intervention   1 797
 
Much simpler :)

 import datetime print "today:", datetime.date.today() print "yesterday:", datetime.date.today()-datetime.timedelta(1) 

--
Without politeness, we would only come together to fight.
We must therefore either live alone or be polite.
Alphonse Karr
2
Eclipsse
 
Ça fonctionne parfaitement !!

Merci pour les conseils

Eclipsse
0
getnate Posted messages 3 Registration date   Status Membre
 
Hey,
so how do you indicate the date from one month ago?
0
sebsauvage Posted messages 33284 Registration date   Status Modérateur Last intervention   15 684 > getnate Posted messages 3 Registration date   Status Membre
 
Use timedelta (see the Python documentation).
0
teebo Posted messages 33570 Registration date   Status Modérateur Last intervention   1 797
 
Hello
You are -1 on today's date :)
--
Without politeness, we would only meet to fight.
So one must either live alone or be polite.
Alphonse Karr
0
tata-titi Posted messages 43 Status Membre 11
 
It's not that simple if you want a date in the format "20070702"!
--------------------------------------------------------------------------------

today = time()

# 86400 = 24 h x 3600 seconds => 1 day in seconds!
yesterday = today - 86400
formatted = gmtime(yesterday)

if len(str(formatted[1]))<2:
first = "0" + str(formatted[1])
else:
first = str(formatted[1])
if len(str(formatted[2]))<2:
second = "0" + str(formatted[2])
else:
second = str(formatted[2])

concat = str(formatted[0]) + first + second

=== > because otherwise it could result in "200772"!!!
0
sebsauvage Posted messages 33284 Registration date   Status Modérateur Last intervention   15 684
 
If you want a date in the format "20070702"!

See strftime()

--
“La vie est courte - Vous avez besoin de Python” -- Bruce Eckel, member of the ANSI C++ committee
0