Home  >  Article  >  Backend Development  >  Python time processing datetime example

Python time processing datetime example

巴扎黑
巴扎黑Original
2017-05-21 11:11:431291browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn