输入某年某月某日,判断这一天是这一年的第几天?
• 例子:
输入的日期为2017/03/05,是2017年的第几天?
输入的日期为2012/03/05,是2012年的第几天?
• 分析:
1. 每个月份的天数不同
2. 闰年与平年的2月份天数不同
3. 闰年判断:
四年一闰,百年不闰,四百年再闰
版本1.0
'''
功能:输入某年某月某日,判断这是一年中的第几天
版本:1.0
'''
from datetime import datetime
def main():
input_date_str = input('请输入日期(yyyymmdd)')
input_date = datetime.strptime(input_date_str ,'%Y%m%d')
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))
if __name__=='__main__':
main()
版本2.0
增加功能:用列表替换元组
'''
功能:输入某年某月某日,判断这是一年中的第几天
版本:2.0
'''
from datetime import datetime
def is_leap_year(year):
'''
判断year是否是闰年
是:返回true
否:返回false
'''
is_leap = False
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
is_leap = True
return is_leap
def main():
input_date_str = input('请输入日期(yyyymmdd)')
input_date = datetime.strptime(input_date_str ,'%Y%m%d')
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
print('二月份天数',days_in_month_list [1])
days = sum(days_in_month_list [:month-1]) + day
# if month > 2 and is_leap_year(year) :
# days += 1
print('这是{}年的第{}天。'.format(year,days))
if __name__=='__main__':
main()
3.0版本
增加功能:将月份划分为不同的集合再操作
集合
• Python中的集合(set)类型同数学中的集合概念一致,即包含0或多个
数据项的无序组合
• 集合中的元素不可重复
• 集合是无序组合,没有索引和位置的概念
• set()函数用于集合的生成,返回结果是一个无重复且排序任意的集合
• 集合通常用于表示成员间的关系、元素去重等
'''
功能:输入某年某月某日,判断这是一年中的第几天
版本:3.0
'''
from datetime import datetime
def is_leap_year(year):
'''
判断year是否是闰年
是:返回true
否:返回false
'''
is_leap = False
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
is_leap = True
return is_leap
def main():
input_date_str = input('请输入日期(yyyymmdd)')
input_date = datetime.strptime(input_date_str ,'%Y%m%d')
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 = 0
days += day
for i in range(1,month): #起始值从1开始
if i in _30_days_month_set :
days += 30
elif i in _31_days_month_set :
days += 31
else:
days += 28
if month > 2 and is_leap_year(year) :
days += 1
print('这是{}年的第{}天。'.format(year,days))
if __name__=='__main__':
main()
4.0版本
增加功能:将月份及其对应天数通过字典表示
字典
• 字典类型(dict)是“键--值”数据项的组合,每个元素是一个键值对
• 如:身份证号(键)--个人信息(值)
• 字典类型数据通过映射查找数据项
• 映射:通过任意键查找集合中的值的过程
• 字典类型以键为索引,一个键对应一个值
• 字典类型的数据是无序的
'''
功能:输入某年某月某日,判断这是一年中的第几天
版本:4.0
'''
from datetime import datetime
def is_leap_year(year):
'''
判断year是否是闰年
是:返回true
否:返回false
'''
is_leap = False
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
is_leap = True
return is_leap
def main():
input_date_str = input('请输入日期(yyyymmdd)')
input_date = datetime.strptime(input_date_str ,'%Y%m%d')
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}
#创建字典
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 = 0
days += day
for i in range(1,month): #起始值从1开始
days += month_day_dict [i]
if month > 2 and is_leap_year(year) :
days += 1
print('这是{}年的第{}天。'.format(year,days))
if __name__=='__main__':
main()
备注:
Python提供了一个可以直接获取“第几天”
的方法,一行代码就能完成
• 自己通过查阅官方文档找到实现方法
• 提示
strftime()中提供了许多格式化日期字符串的
操作
https://docs.python.org/3/library/time.html#time.strftime