本文是学习齐老师的《python全栈工程师》课程的笔记,欢迎学习交流。同时感谢齐老师的精彩传授!
01.calendar模块:
# 交互模式下:
>>> import calendar
>>> print(calendar.calendar(2019))
# 交互模式下:
>>> import calendar
>>> calendar.firstweekday() # 一周中的第一天,默认是0,即周一
0
>>> calendar.setfirstweekday(6) # 将周日设置为一周中的第一天
>>> calendar.firstweekday()
6
>>> print(calendar.calendar(2019))
>>> print(calendar.month(2019, 11))
将输出如下结果,(注意:前面已经将周日设置为一周第一天了)
以列表的形式得到某个月的日历:
# 获取某个月的第一天是星期几,这个月共几天
>>> calendar.monthrange(2019, 11)
(4, 30) # 4表示11月份的第一天是星期四,11月份共有30天
>>> calender.isleap(2019)
False # 2019不是闰年
>>> calendar.isleap(2000)
True # 2000是闰年
>>> calendar.leapdays(2000, 2019)
5 # 表示2000年到2019年之间有5个闰年
>>> calendar.weekday(2019, 11, 11)
0 # 表示2019年11月11号是星期一,0是星期一,6是星期日
# 交互模式下:
>>> import time
>>> time.gmtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
>>> time.time()
157412760.192193
>>> time.localtime()
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=9, tm_hour=11, tm_min=9, tm_se
c=0, tm_wday=0, tm_yday=343, tm_isdst=0)
>>> time.gmtime()
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=9, tm_hour=3, tm_min=9, tm_sec
=5, tm_wday=0, tm_yday=343, tm_isdst=0)
>>> time.gmtime().tm_year
2019
类型转换:
# 交互模式下:
# 转换为时间戳
>>> time.mktime(time.localtime())
1575597358.0
# 转换为字符串
>>> time.asctime(time.localtime())
'Fri Dec 6 09:56:18 2019'
>> time.asctime(time.gmtime())
'Fri Dec 6 01:57:31 2019'
>>> time.ctime()
'Fri Dec 6 09:58:36 2019'
>>> time.ctime(time.time())
'Fri Dec 6 09:59:17 2019'
# 时间戳转换为字符串
>>> time.ctime(123456789.123)
'Fri Nov 30 05:33:09 1973'
格式化时间字符串
# 交互模式下
>>> time.strftime('%Y-%m-%d %H:%I:%S', time.localtime())
'2019-12-06 10:10:02'
>>> time.strftime('%Y年%m月%d日 %M时%I分%S秒', time.localtime())
'2019年12月06日 10时19分04秒'
# 如果有报错:UnicodeEncodeError: 'locale' codec can't encode character '\u5e74'
# in position 2 : encoding error 的同学,可以用下面这个:
>>> time.strftime('%Y{y}%m{m}%d{d} %H{h}%M{f}%S{s}').format(y='年', m='月', d='日', h='时', f='分', s='秒')
'2019年12月06日 10时19分04秒'
>>> time.strptime('2019年11月11日 11时11分11秒', '%Y年%m月%d日 %H时%M分%S秒')
time.struct_time(tm_year=2019, tm_mon=11, tm_mday=11, tm_hour=11, tm_min=11, tm_
sec=11, tm_wday=0, tm_yday=315, tm_isdst=-1)
02.datetime模块:
datetime.time:
# 交互模式下:
>>> import datetime
>>> t = datetime.time(11, 12, 13,777)
>>> t.hour
11
>>> t.second
13
>>> t.__format__('%H:%M:%S')
'11:12:13'
>>> t.__format__('%S:%M:%H')
'13:12:11'
>>> t.strftime('%H:%M:%S')
'11:12:13'
>>> t.isoformat()
'11:12:13.000777'
>>> t.replace(hour=15)
datetime.time(15, 12, 13, 777)
>>> t
datetime.time(11, 12, 13, 777)
datetime.date:
# 交互模式:
>>> t = datetime.date.today()
>>> t
datetime.date(2019, 12, 6)
>>> t.year
2019
>>> t.timetuple()
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=6, tm_hour=0, tm_min=0, tm_sec
=0, tm_wday=4, tm_yday=340, tm_isdst=-1)
>>> t.toordinal()
737399
>>> t.weekday()
4
>>> t.strftime('%Y%m%d')
'20191206'
>>> t.strftime('%Y-%m-%d')
'2019-12-06'
datetime.datetime:
# 交互模式下:
>>> a = datetime.datetime.now()
>>> a
datetime.datetime(2019, 12, 6, 15, 40, 39, 242713)
>>> a.date()
datetime.date(2019, 12, 6)
>>> a.time()
datetime.time(15, 40, 39, 242713)
>>> a.utctimetuple()
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=6, tm_hour=15, tm_min=40, tm_s
ec=39, tm_wday=4, tm_yday=340, tm_isdst=0)
>>> datetime.datetime.strptime('2019-12-06 15:42:20', '%Y-%m-%d %H:%M:%S')
datetime.datetime(2019, 12, 6, 15, 42, 20)
>>> a.timestamp()
1575618039.242713
>>> print(datetime.datetime.fromtimestamp(a.timestamp()))
2019-12-06 15:40:39.242713
datetime.timedelta:
# 交互模式:
>>> delta_h = datetime.timedelta(hours=2)
>>> delta_h
datetime.timedelta(seconds=7200)
>>> str(delta_h)
'2:00:00'
>>> delta_d = datetime.timedelta(days=1)
>>> str(delta_d)
'1 day, 0:00:00'
>>> today = datetime.date.today()
>>> today
datetime.date(2019, 12, 6)
>>> yesterday = today - delta_d
>>> yesterday
datetime.date(2019, 12, 5)
>>> tomorrow = today + delta_d
>>> tomorrow
datetime.date(2019, 12, 7)
>>> tomorrow - yesterday
datetime.timedelta(days=2)
>>> work = datetime.timedelta(hours=8)
>>> meeting = datetime.timedelta(hours=1)
>>> meeting / work
0.125
练习:
- 例题1:计算上周一和周日的日期
# 交互模式下:
>>> import datetime
>>> today = datetime.date.today() # 获取今天的日期
>>> today_weekday = today.isoweekday() # 获取今天星期几 [1, 7]
>>> today_weekday
5
>>> last_sunday = today - datetime.timedelta(days=today_weekday) # 今天减去几天(星期几就是几天),获取上周日日期
>>> last_sunday
datetime.date(2019, 12, 1)
>>> last_monday = last_sunday - datetime.timedelta(days=6) # 上周日减去6天就是上周一
>>> last_monday
datetime.date(2019, 11, 25)
小编参考:
import calendar
import datetime
today = datetime.date.today() # 获取今天日期
m_range = calendar.monthrange(today.year, today.month)
month_days = m_range[1] # 获取这个月份的天数
print('本月最后一天的日期:{0}-{1}-{2}'.format(today.year, today.month, month_days))
print('本月的天数:', month_days)