天天看点

Python零基础学习笔记(四十)—— datetime和Calendar

import datetime
import time
'''
datetime比time高级了不少,可以理解为date time基于time进行了封装,
提供了各种实用的函数,date time模块的接口更为直观,更容易调用

模块中的类:
datetime    同时有时间和日期
timedelta   主要用于计算时间跨度
tzinfo      时区相关
time        只关注时间
date        只关注日期
'''
#获取当前时间
d1 = datetime.datetime.now()
print(d1)
#
d2 = datetime.datetime(1995, 4, 28,10,23,34,123355)
print(d2)
#
d3 = datetime.datetime.time(d1)
print(d3)
#将时间转换为字符串
#d4 = d1.strptime("%Y-%m-%d %X")
#print(d4)

#时间加减
d5 = d1 - d2
print(d5)
#间隔的天数
print(d5.days)
#间隔天数除外的秒数
print(d5.seconds)

           
import calendar
'''
日历
'''
#使用
print(calendar.month(2019, 7))

#返回指定年的日历
print(calendar.calendar(2019))

#判断闰年,返回True
print(calendar.isleap(2000))

#返回某个月的weekday的第一天和这个月的天数
print(calendar.monthrange(2018, 12))

#返回某个月以每一周为元素的列表
print(calendar.monthcalendar(2017, 12))