python时间格式化、运行时长计算
1.格式化:
import time timeStr=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) #格式化为:2018-07-17 19:03:36形式 或者 timeStr=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) #格式化为:2018-07-17 19:03:36形式 print(timeStr)
2.运行时长:
import time start=time.time() print('the program starts at: %s' % time.time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(start))) #do some work here end=time.time() print('the program ends at: %s' % time.time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(end))) collapse=end-start print('it takes %s seconds to finish the program.' % collapse)