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

Python time processing datetime example

巴扎黑
Release: 2017-05-21 11:11:43
Original
1344 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template