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年开始到指定日期过去多少天,也可以用天数来查询日期
[root@lpc 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



 

posted @ 2018-05-27 16:50  Leonardo-li  阅读(318)  评论(0编辑  收藏  举报