Home > Article > Backend Development > Python time processing datetime example
There are many chapters in Python Cook's book that introduce the use of a certain library or realize some complex functions by combining multiple functions. I directly skipped some chapters on file processing in the previous chapter and went directly to the chapter on time operations.
At the same time, datetime is also briefly introduced. Because there are many things that you need to use yourself, it is most effective to look for help.
Example:
Calculate the previous Friday and output it.
Answer:
Copy code The code is as follows:
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 result:
Friday, 09-May-2008
The above is the detailed content of Python time processing datetime example. For more information, please follow other related articles on the PHP Chinese website!