Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confused with date 1

Status
Not open for further replies.

IPGuru

Vendor
Jun 24, 2003
8,391
GB
I know this is probably Very simple

i want to calculate last weeks date & return it as a string in the format YYYY-MM-DD

So far I have found lots of information on the datetime lib but I am not sure what exactly I am doing with it yet

 
Code:
>>> import datetime
>>> today = datetime.date.today()
>>> today.isoformat()
'2010-05-14'
>>> last_week = today - datetime.timedelta(days=7)
>>> last_week.isoformat()
'2010-05-07'
>>>
 
Many thanks
I have found something myself using time instead but i think yours is more readable (isn't it always the way that you think of an answer just after posting)
Just in case anyone is interested

Code:
import time
today=time.time()
lastweek=time.gmtime(today-(7*3600*24) # days * sec in 24H
date=time.strftime("%Y-%m-%d")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top