一、datetime模块

 

1date 日期对象,常用的属性有year,month,day

2time 时间对象,常用的属性有:hour,minute,second,microsecond

3datetime 日期时间对象,常用的属性有year,month,day,hour,minute,second,microsecond

4timedelta 时间间隔,即两个时间点之间的长度

5tzinfo 时区信息对象

 

演示:

 

import datetime

# 1 日期(年份,月份和日期)

date01 = datetime.date.today()

print(date01)

print("年份", date01.year)

print("月份", date01.month)

print("日期", date01.day)

 

# 2 时间(时,分,秒,微秒,时区)

time01 = datetime.time(12, 12, 12, 121212)

print(time01)

print("时:", time01.hour)

print("分:", time01.minute)

print("秒:", time01.second)

print("微妙:", time01.microsecond)

 

# 3 datetime 时间日期(年份、月份、日期、时、分、秒、微妙、时区)

datetime01 = datetime.datetime.now()

print(datetime01)

print("年份", datetime01.year)

print("月份", datetime01.month)

print("日期", datetime01.day)

print("时:", datetime01.hour)

print("分:", datetime01.minute)

print("秒:", datetime01.second)

print("微妙:", datetime01.microsecond)

 

# 4 三种类型的转换

print("datetime中取date:", datetime01.date())

print("datetime中取time:", datetime01.time())

print("datetime组合成datetime:", datetime.datetime.combine(date01,time01))

 

# 5 timeDelta 时间间隔

date01 = datetime.date(2012, 12, 12)

date02 = datetime.date(2008, 5, 27)

print((date01 - date02).days)

 

 

执行结果:

C:\python\python.exe C:/python/demo/file3.py

2018-03-01

年份 2018

月份 3

日期 1

12:12:12.121212

时: 12

分: 12

秒: 12

微妙: 121212

2018-03-01 10:41:33.727941

年份 2018

月份 3

日期 1

时: 10

分: 41

秒: 33

微妙: 727941

datetime中取date: 2018-03-01

datetime中取time: 10:41:33.727941

datetime组合成datetime: 2018-03-01 12:12:12.121212

1660

 

Process finished with exit code 0