3 calender python_python3笔记二十一:时间操作datetime和calendar

一:学习内容

datetime

calendar

二:datetime

1.模块说明:可以理解为datetime基于time进行了封装,提供了各种使用的函数,datetime模块的接口更直接,更容易调用

2.datetime模块中的类:

datetime    同时有时间和日期

timedelta   主要用于计算时间的跨度

tzinfo      时区相关

time        只关注时间

date        只关注日期

3.获取当前时间

import datetime

d1 = datetime.datetime.now()

print(d1)

print(type(d1))

252ae00a81886fd12bea46396cb47688.png

4.获取指定的时间

d2 = datetime.datetime(1991, 10, 8, 10, 28, 20, 123456)

print(d2)

d3071e97dbcd60ae5da495453c354907.png

5.将时间(datetime.datetime类型)转为字符串

import datetime

d1 = datetime.datetime.now()

d3 = d1.strftime("%Y-%m-%d %X")

print(d3)

print(type(d3))

e5de8c4383fa8e78af0d008c4440627c.png

6.将格式化字符串转成datetime类型

import datetime

d1 = datetime.datetime.now()

d3 = d1.strftime("%Y-%m-%d %X")

#注意:转换的格式要与字符串一致

d4 = datetime.datetime.strptime(d3, "%Y-%m-%d %X")

print(d4)

print(type(d4))

f378abf582123e29be2b9fdee5912a89.png

7.时间间隔

d5 = datetime.datetime(1999, 10, 1, 10, 28, 20, 123456)

d6 = datetime.datetime.now()

d7 =d6 - d5

print(d7)                #7285 days, 6:23:31.611593

print(type(d7))

5643fa6b0d66d2b47b7c2cc88c41b0a0.png

8.间隔的天数

d5 = datetime.datetime(1999, 10, 1, 10, 28, 20, 123456)

d6 = datetime.datetime.now()

d7 =d6 - d5

print(d7.days)

5b3fce60590e34341389761d9bc82360.png

9.间隔天数除外的秒数

d5 = datetime.datetime(1999, 10, 1, 10, 28, 20, 123456)

d6 = datetime.datetime.now()

d7 =d6 - d5

print(d7)                              #7285 days, 6:26:52.858784

print(d7.seconds)               #23212,即上面的除天数外的时间6*3600+26*60+52=23212

8adf74de8357ce91d96bbd62543f2ce9.png

三:calendar

1.模块说明:日历模块,需要导入包:import calendar

2.返回指定某年某月的日历

print(calendar.month(2019,11))

3902c652aaf62e125b797b8ecf185a37.png

3.返回指定年的日历

print(calendar.calendar(2017))

c90f92fc2d8565a6012b87cf4b84790f.png

4.判断闰年,是返回True,否则返回False

print(calendar.isleap(2010))

b131914b558903e00c320caeade20364.png

5.返回某个月的weekday的第一天和这个月所有的天数

print(calendar.monthrange(2019,8))

3fb8a5974bf030b9d6e9eef132ef6db0.png

6.返回某个月以每一周为元素的列表

print(calendar.monthcalendar(2019,7))

e1702d868574fd7145c41b83f3e10d7e.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值