常见模块

import time
print("# 1 time() :返回当前时间的时间戳")
print(time.time())
print("#2 localtime([secs])将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。")
print(time.localtime())
print(time.localtime().tm_year) #可以单独打印
print("#3 gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。")
print(time.gmtime())
print("#4 mktime(t) : 将一个struct_time转化为时间戳。")
print(time.mktime(time.localtime()))
print("#5 asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。如果没有参数,将会将time.localtime()作为参数传入。")
print(time.asctime())
print("#6 ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。")
print(time.ctime())
print(time.ctime(time.time()))
print("# 7 strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个元素越界,ValueError的错误将会被抛出。")
print(time.strftime("%Y-%m-%d %X", time.localtime()))
print("# 8 time.strptime(string[, format])把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。")
print(time.strptime('2019-03-05 09:37:17', '%Y-%m-%d %X'))
Time
# 1 time() :返回当前时间的时间戳
1552886460.8447163
#2 localtime([secs])将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=18, tm_hour=13, tm_min=21, tm_sec=0, tm_wday=0, tm_yday=77, tm_isdst=0)
2019
#3 gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=18, tm_hour=5, tm_min=21, tm_sec=0, tm_wday=0, tm_yday=77, tm_isdst=0)
#4 mktime(t) : 将一个struct_time转化为时间戳。
1552886460.0
#5 asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。如果没有参数,将会将time.localtime()作为参数传入。
Mon Mar 18 13:21:00 2019
#6 ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
Mon Mar 18 13:21:00 2019
Mon Mar 18 13:21:00 2019
# 7 strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个元素越界,ValueError的错误将会被抛出。
2019-03-18 13:21:00
# 8 time.strptime(string[, format])把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=5, tm_hour=9, tm_min=37, tm_sec=17, tm_wday=1, tm_yday=64, tm_isdst=-1)
result

 

猜你喜欢

转载自www.cnblogs.com/telma/p/10551635.html