python datetime模块

该模块的时间有限时限:1 - 9999

dir(datetime) 

python datetime模块

 from datetime import datetime, timedelta, timezone

 dt = datetime.now()
t = dt.timestamp() dt1 = datetime.fromtimestamp(t) utc_dt = datetime.utcfromtimestamp(t) tz_utc_8 = timezone(timedelta(hours=8))
utc_dt1 = dt.replace(tzinfo=tz_utc_8) str1 = dt.strftime("%Y-%m-%d %X")
str2 = dt.ctime() dt2 = datetime.strptime('2016-11-27', "%Y-%m-%d") st = dt.timetuple() dt3 = dt + timedelta(days=2, hours=1)

python datetime模块

练习:

(1)现在时间
(2)指定时间
(3)闰年判定
(4)今年第几天
(5)今天第几周
(6)程序运行时间
(7)判定星期几
(8)时间日期比较
 "(1)现在时间"
from datetime import datetime now1 = datetime.now()
now2 = datetime.today()
print(now1)
print(now2) "(2)指定时间"
dt = datetime(2016, 12, 3, 21, 17)
print(dt) "(3)闰年判定"
import calendar if calendar.isleap(2016):
print("leap year") "(4)今年第几天"
import time print(time.localtime().tm_yday)
print(time.strftime("%j")) "(5)今天第几周"
print(time.strftime("%W")) "(6)程序运行时间"
t1 = time.time()
t2 = time.time()
print("run time: ", t2 - t1) t1 = time.clock()
t2 = time.clock()
print("{}".format(t2)) "(7)判定星期几"
"0-6"
print(datetime.now().weekday()) "(8)时间日期比较" dt1 = datetime(2016, 12, 3)
dt2 = datetime(2016, 12, 4)
print(dt1 > dt2)
print(dt1 == dt2)
print(dt1 < dt2)
print(dt1 is dt2)
print(dt1 is not dt2)
 
上一篇:你使用PetaPoco必须知道的事情


下一篇:HDU 3746 (KMP求最小循环节) Cyclic Nacklace