python——日历

Posted Leonardo-li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python——日历相关的知识,希望对你有一定的参考价值。

 1 #!/usr/bin/env/ python3
 2 # -*- codinf: utf-8 -*-
 3 # year: 2018
 4 # day: 138
 5 #   --> XX/XX
 6 # year: 2000
 7 # day: 63
 8 # --> 03/03
 9 # year: 2014
10 # day: 63
11 #   --> 03/04
12 def is_leap_year(year):
13     if year % 100 != 0:
14        if year % 4 == 0:
15           return 1
16     else:
17        if year % 400 == 0:
18           return 1
19     return 0
20 
21 def get_days_by_year_month(year,month):
22     days = [ # 01  02  03  04  05  06  07  08  09 10 11 12
23               [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
24               [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
25     ]
26     return days[is_leap_year(year)][month-1]
27 
28 def get_month_day_by_year_days(year, days):
29     month, day = 1, 1
30     ###
31     while True:
32         d = get_days_by_year_month(year, month)
33         if days <= d:
34            day = days
35            break
36         days -= d
37         month += 1
38     ###
39 
40     return month, day
41 def get_days_by_year_month_day(year, month, day):
42     # 2018/3/18  -->  31+28+18
43     days = day
44     for i in range(1, month):
45         days += get_days_by_year_month(year, i)
46     return days
47 
48 if __name__ == __main__:
49     year  = int(input(year:  ))
50     month = int(input(month: ))
51     day   = int(input(day:   ))
52     days  = get_days_by_year_month_day(year, month, day)
53     print(%d/%02d/%02d --> %dday(s) in %d year. %(year, month, day, days, year))
54     days  = int(input(day: ))
55     month, day = get_month_day_by_year_days(year, days)
56     print(%d of %d --> %02d/%02d %(days, year, month, day))

执行结果:
可以查询2018年开始到指定日期过去多少天,也可以用天数来查询日期
[[email protected] 05-4-日历]# python3 rili.py
year:  2018
month: 5
day:   27
2018/05/27 --> 147day(s) in 2018 year.
day: 148
148 of 2018 --> 05/28



 














以上是关于python——日历的主要内容,如果未能解决你的问题,请参考以下文章

python 练习 输入年月 输出当年当月日历 求代码 重赏

python打印日历

Python基础之时间日历

Python实战练习——打印日历教程

常用python日期日志获取内容循环的代码片段

python 有用的Python代码片段