python——日曆

來源:互聯網
上載者:User

標籤:執行   turn   pytho   main   name   多少   print   range   指定   

 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: 201410 # day: 6311 #   --> 03/0412 def is_leap_year(year):13     if year % 100 != 0:14        if year % 4 == 0:15           return 116     else:17        if year % 400 == 0:18           return 119     return 020 21 def get_days_by_year_month(year,month):22     days = [ # 01  02  03  04  05  06  07  08  09 10 11 1223               [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, 130     ###31     while True:32         d = get_days_by_year_month(year, month)33         if days <= d:34            day = days35            break36         days -= d37         month += 138     ###39 40     return month, day41 def get_days_by_year_month_day(year, month, day):42     # 2018/3/18  -->  31+28+1843     days = day44     for i in range(1, month):45         days += get_days_by_year_month(year, i)46     return days47 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——日曆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.