In many chapters of Python Cookbook PDF, it is focused on how to use a lib or how to combine multiple functions to implement a complex functionality. Here we skip all these details and go straight into how to process time in python.
At the meantime, we simple introduce how to use datetime.
Examples:
how the get the previous Friday and output it.
Solutions:
code:
import datetime, calendar
lastFriday = datetime.date.today( )
oneday = datetime.timedelta(days=1)
lastFriday -= oneday
while lastFriday.weekday( ) != calendar.FRIDAY:
lastFriday -= oneday
print lastFriday.strftime('%A, %d-%b-%Y')
output:
Friday, 09-May-2008
Post a Comment