python之时间日期calendar

 calendar是与日历相关的模块,calendar模块文件里定义了很多类型,主要有Calendar,TextCalendar以及HTMLCalendar类型。其中,Calendar是TextCalendar与HTMLCalendar的基类。该模块文件还对外提供了很多方法,例如:calendar,month,prcal,prmonth之类的方法...

1.calendar()获取指定年份的日历字符串

import calendar

calen=calendar.calendar(2018)
print(calen)

2.month()获取指定月份的日历字符串

import calendar

calen=calendar.month(2018,8)
print(calen)
结果:
[python@master calendar]$ python3 2.py 
    August 2018
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

3.isleap()检测年份是否是润年

import calendar

calen1=calendar.isleap(2000)
calen2=calendar.isleap(1900)
print(calen1)
print(calen2)
结果:
[python@master calendar]$ python3 3.py 
True
False

4.leapdays()检测指定年限内润年的数量

import calendar

calen1=calendar.leapdays(1949,2018)
print(calen1)
结果:
[python@master calendar]$ python3 4.py 
17

5.monthrange() 获取指定月份的信息

import calendar

calen1=calendar.monthrange(2018,8)
print(calen1)
结果:
[python@master calendar]$ python3 5.py 
(2, 31)

6.weekday ()根据指定的年月日计算星期几

import calendar

calen1=calendar.weekday(2018,8,27)
print(calen1)
结果:
[python@master calendar]$ python3 6.py 
0

7.timegm() 将时间元组转化为时间戳

import calendar

tps = (2018,8,27,11,35,0,0,0)
result = calendar.timegm(tps)
print(result)
结果:
[python@master calendar]$ python3 7.py 
1535369700

还有其他函数:

 生活总是如此艰难!

猜你喜欢

转载自www.cnblogs.com/hello-wei/p/9541249.html