1、time.time():返回当前时间戳,即1970年至当前时间的秒数(单位:秒)
.
2、time.localtime():没有参数时:返回本地当前时间的时间元组
time.localtime():有参数时:接收时间戳,并返回时间元祖
.
3、time.mktime():接收时间元组,返回对应的秒数
now_time = time.localtime()
print(now_time)
print(time.mktime(now_time))
.
4、time.gmtime():接收秒数,返回对应的时间元组
注:返回的时间元组为本初子午线的时间(中国为东八区,若想获得东八区时间,可先加上 8 * 60 * 60)
time.localtime()返回本地时间元组,无需补时间差
now_time = time.time()
print(now_time)
print(' gmtime:', time.gmtime(now_time))
print('localtime:', time.localtime(now_time))
.
5、time.strftime():接收时间元组,并格式化成字符串
格式化占位符含义:
%Y:四位数的年份表示(000-9999)
%m:月份(01-12)
%d:月内中的一天(0-31)
%H:24小时制小时数(0-23)
%M:分钟数(00=59)
%S:秒(00-59)
now_time = time.localtime()
print(now_time)
print(time.strftime('%Y-%m-%d %H:%M:%S', now_time))
.
6、time.strptime():将字符串按照格式化字符串转化为时间元祖
now_time = '2020-03-22 10:16:59'
print(time.strptime(now_time, '%Y-%m-%d %H:%M:%S'))
.
7、time.sleep(second):线程睡眠second秒
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
time.sleep(5)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))