Home  >  Article  >  Backend Development  >  Python simple method to format time [strftime function]

Python simple method to format time [strftime function]

高洛峰
高洛峰Original
2017-01-12 15:26:041633browse

The example in this article describes the simple method of formatting time in Python. It is shared with everyone for your reference. The details are as follows:

Walker often uses the current time and relative time to count the efficiency of program execution. Simple Remember it for easy copying.

>>> import time
>>> startTime = time.time()
>>> '%.2fs' % (time.time() - startTime) #相对时间
'24.51s'
>>> '{:.2f}s'.format(time.time() - startTime) #相对时间
'37.55s'
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) #格式化当前时间
'2016-01-05 10:34:06'

I hope this article will be helpful to everyone in Python programming.

For more Python simple methods to format time [strftime function], please pay attention to the PHP Chinese website for related articles!

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