0、准备工作
from _datetime import datetime
def is_leap_year(year):
"""
判断year是否为闰年
是,返回true
否,返回false
:param year:
:return:
"""
is_leap = False
if(year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
is_leap = True
return is_leap
if __name__ == '__main__':
main()
1、元组
"""
作者:Jomly
版本:1.0
日期:2018/01/10
功能:输入某年某月某日,判断这一天是这一年的哪一天(元组)
"""
from _datetime import datetime
def main():
"""
主函数
:return:
"""
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str, '%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
# 计算之前每月的天数总和,以及当前的月份天数
days_in_month_tup = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
days = sum(days_in_month_tup[:month - 1]) + day
# 判断闰年
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
if month > 2:
days += 1
print('这是第{}天'.format(days))
2、列表
"""
作者:Jomly
版本:2.0
日期:2018/01/10
功能:输入某年某月某日,判断这一天是这一年的哪一天
2.0 新增功能:用列表替换元组
"""
def main():
"""
主函数
:return:
"""
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str, '%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
# 计算之前每月的天数总和,以及当前的月份天数
days_in_month_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_leap_year(year) :
days_in_month_list[1] = 29
days = sum(days_in_month_list[:month - 1]) + day
print('这是{}年的第{}天'.format(year, days))
3、集合
def main():
"""
主函数
:return:
"""
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str, '%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
# 包含30天的月份集合
_30_days_month_set = {4, 6, 9, 11}
_31_days_month_set = {1, 3, 5, 7, 8, 10, 12}
# 初始化值
days = day
for i in range(1, month):
if i in _30_days_month_set:
days += 30
elif i in _31_days_month_set:
days += 31
else:
days += 28
if is_leap_year(year) and month > 2:
days += 1
print('这是{}年的第{}天'.format(year, days))
4.1、Map(月份为key)
def main():
"""
主函数
:return:
"""
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str, '%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
month_day_dict = {
1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31
}
# 初始化值
days = day
for i in range(1, month):
days += month_day_dict[i]
if is_leap_year(year) and month > 2:
days += 1
print('这是{}年的第{}天'.format(year, days))
4.2、Map(天数为key)
def main():
"""
主函数
:return:
"""
input_date_str = input('请输入日期(yyyy/mm/dd):')
input_date = datetime.strptime(input_date_str, '%Y/%m/%d')
print(input_date)
year = input_date.year
month = input_date.month
day = input_date.day
day_month_dict = {
30: {4, 6, 9, 11},
31: {1, 3, 5, 7, 8, 10, 12},
28: {2},
29: {2}
}
# 初始化值
days = day
# 需要累计的月份集合
account_months = list(range(1, month))
for per_month_value in account_months:
for item, value in day_month_dict.items():
if per_month_value in value:
# print(item)
days += item
if is_leap_year(year) and month > 2:
days += 1
print('这是{}年的第{}天'.format(year, days))