Python time date 模組
時間的三種形式:
1. 時間戳: 以整型或者浮點型表示, 以秒為單位的時間間隔,時間基礎值是從1970年1月1號0點 time.time()
2. 元組:這個元組有9個整型內容(year, month, day,hour,minutes,second,week,julia day, falg(1,-1,0))
3.格式化字串:%Y 4位的年份 %m %d
import time
c=time.time()
print c
t=time.gmtime(c)
print t
b=time.localtime()
print b
f=time.mktime(b)
print f
d=time.asctime(b)
print d
g=time.ctime(c)
print g
h=time.strftime("%Y-%m-%d %H:%M:%S",b)
print h
k=time.strptime(h,"%Y-%m-%d %H:%M:%S")
print k
1542419970.49
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=17, tm_hour=1, tm_min=59, tm_sec=30, tm_wday=5, tm_yday=321, tm_isdst=0)
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=17, tm_hour=9, tm_min=59, tm_sec=30, tm_wday=5, tm_yday=321, tm_isdst=0)
1542419970.0
Sat Nov 17 09:59:30 2018
Sat Nov 17 09:59:30 2018
2018-11-17 09:59:30
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=17, tm_hour=9, tm_min=59, tm_sec=30, tm_wday=5, tm_yday=321, tm_isdst=-1)
time.sleep(3)
time.clock() : 返回當前程式的cpu時間
datetime:
模組中的類:
datetime類: 同時有日期和時間
timedeltam,
tzinfo,
time,
date
import datetime
curtime = datetime.datetime.now()
print curtime
#get the specific time
a= datetime.datetime(2011,8,10,19)
print a
b=curtime.strftime("%Y-%m-%d %X")
print b
2018-11-17 10:22:39.101000
2011-08-10 19:00:00
2018-11-17 10:22:39
2655 days, 15:22:39.101000
Calendar 模組:
import calendar
print calendar.calendar(2018)
print calendar.month(2018,1)
print calendar.isleap(2018)