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

Python time processing datetime example

高洛峰
高洛峰Original
2017-01-12 15:23:181269browse

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:

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

For more articles related to Python time processing datetime examples, please pay attention to 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