本文共 4067 字,大约阅读时间需要 13 分钟。
醉里挑灯看剑,梦回吹角连营。八百里分麾下炙,五十弦翻塞外声。沙场秋点兵。 马作的卢飞快,弓如霹雳弦惊。了却君王天下事,赢得生前身后名。可怜白发生!
print(time.time())
>>>1539847769.6119404
print(time.localtime())
>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=18, tm_hour=15, tm_min=35, tm_sec=0, tm_wday=3, tm_yday=291, tm_isdst=0)
print(time.mktime(time.localtime()))
>>>1539849828.0
注意和time.time()的区别:print(time.time())
1539849828.090503
print(time.strftime("%Y-%m-%d %H:%M:%S"))
>>>2018-10-18 15:56:42
print(time.strptime("2018-10-18 15:56:42","%Y-%m-%d %H:%M:%S"))
>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=18, tm_hour=15, tm_min=56, tm_sec=42, tm_wday=3, tm_yday=291, tm_isdst=-1)
print(time.gmtime(time.time()))
>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=18, tm_hour=8, tm_min=10, tm_sec=16, tm_wday=3, tm_yday=291, tm_isdst=0)
附一:
附二:time时间之间的转换
datetime模块定义了5个类,分别是
print(datetime.date(2017,12,1))
>>>2017-12-01
print(datetime.date.today())
>>>2018-10-18
a = datetime.date.today()
print(datetime.date.isoweekday(a))
>>>4
a = datetime.date.today()
print(datetime.date.weekday(a))
>>>3
print(datetime.date.fromtimestamp(1539847769))
>>>2018-10-18
a = datetime.date.today()
print(a.timetuple())
>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=291, tm_isdst=-1)
a = datetime.date.today()
print(a.replace(year=2010))
>>>2010-10-18
time类有5个参数,datetime.time(hour,minute,second,microsecond,tzoninfo)
print(datetime.time(8,5,32))
print(datetime.time(8,5,32,32))
>>>08:05:32
>>>08:05:32.000032
cls.strftime("时间格式")
指定时间格式a= datetime.time(8,5,32,32)
print(a.strftime("%H:%M"))
>>>08:05
cls.replace()
替换时分秒a= datetime.time(8,5,32,32)
print(a.replace(hour=18))
>>>18:05:32.000032
datetime.time.tzname()
返回时区名字:略datetime.time.utcoffset()
返回时区的时间偏移量:略datetime模块类的使用大同小异,有很多方法我们可能这辈子都用不上,datetime类的方法比较多,在这里我就捡重点的列一下:
print(datetime.datetime.now())
>>>2018-10-18 19:15:17.691425
print(datetime.datetime.now().date())
>>>2018-10-18
print(datetime.datetime.now().time())
>>>19:16:54.497104
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
>>>2018-10-18 19:21:13
a = datetime.datetime.strptime("2018-10-18 19:21:37",'%Y-%m-%d %H:%M:%S')
print(a,type(a))
>>>2018-10-18 19:21:37 <class 'datetime.datetime'>
datetime.datetime.timedelta用于计算两个日期之间的差值:
import datetimea = datetime.datetime(2015, 10, 3)b = datetime.datetime.now()res1 = (b - a)res2 = (b - a).days // 取时间差天数res3 = (b - a).seconds // 取时间差时分秒部分的秒数总和res4 = (b - a).microseconds // 取时间差 秒数之后的微秒数res5 = (b - a).total_seconds() // 时间差的总秒数print(res1) >>> 1111 days, 19:48:35.543523print(res2) >>> 1111print(res3) >>> 71315print(res4) >>> 71315print(res5) >>> 96061715.543523
转载地址:http://eityx.baihongyu.com/