Calculate strings and common data structures specified date is the first few days of the year

Evaluates the specified date is the first few days of the year

def is_leap_year(year):
 """
 判断指定的年份是不是闰年

 :param year:年份
 :return:闰年返回True 平年返回False
 """
 return year % 4 == 0 and year % 100 != 0 or year % 400 == 0

def which_day(year, month, date):
 """
 计算传入的日期是这一年的第几天

 :param year: 年
 :param month: 月
 :param date: 日
 :return: 第几天
 """
 days_of_month = [
  [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
   ][is_leap_year(year)]
 total = 0
 for index in range(month - 1):
  total += days_of_month[index]
 return total + date

def main():
 print(which_day(1980, 11, 30))
 print(which_day(2019, 3, 1))
 print(which_day(1998, 1, 7))
 print(which_day(2020, 2, 29))

if __name__ == '__main__':
 main()

Here Insert Picture Description

Published 96 original articles · won praise 8 · views 4337

Guess you like

Origin blog.csdn.net/weixin_46108954/article/details/104637653