datetime模块+calendar模块

2023-05-16,,

datetime

"""
模块中的类:
datatime 同时有时间和日期
timedelta 主要用于计算时间的跨度
tzinfo 时区相关
time 只关注时间
date 只关注日期 """
import datetime #获取当前时间
d1 = datetime.datetime.now()
print(d1)
print(type(d1)) #获取指定的时间
d2 = datetime.datetime(1999,10,1,10,28,25,564825)
print(d2) #将时间转为字符串
d3 = d1.strftime("%y-%m-%d %X")
print(d3)
print(type(d3)) #将格式化字符串转为datetime对象
#转换的格式要与字符串一致
d4 = datetime.datetime.strptime(d3,"%y-%m-%d %X")
print(d4)
print(type(d4)) d5 =datetime.datetime(1999,10,1,10,28,25,123456)
d6 = datetime.datetime.now()
d7 = d6-d5
print(d7)
print(type(d7))
#间隔的天数
print(d7.days)
#间隔天数除外的秒数
print(d7.seconds)

calendar

import calendar

"""
日历模块
""" #使用 #返回指定某年某月的日历
print(calendar.month(2018,11)) #返回指定年的日历
# print(calendar.calendar(2018)) #闰年返回True, 否则返回False
# print(calendar.isleap(2018)) #返回某个月的weekday的第一天和这个月所有的天数
# print(calendar.monthrange(2018,11)) #返回某个月以每一周为元素的列表
print(calendar.monthcalendar(2018,7))

datetime模块+calendar模块的相关教程结束。

《datetime模块+calendar模块.doc》

下载本文的Word格式文档,以方便收藏与打印。