20221230星期五:
输入指定格式的日期,然后判断这一天是一年中的第几天:
# 方式一:datetime函数:
from datetime import *
da = input("请输入年月日,输入格式为xxxx-xx-xx:")
da1 = datetime.strptime(da[:4] '-1-1','%Y-%m-%d')
da2 = datetime.strptime(da,'%Y-%m-%d')
print(type(da1),da1) # <class 'datetime.datetime'> 2022-01-01 00:00:00
print(type(da2),da2) # <class 'datetime.datetime'> 2022-12-22 00:00:00
print("今天是一年中的第 {} 天".format((da2-da1).days 1),type(da2))
# 方式二:datetime.date函数:
from datetime import *
def findDays():
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入日期:"))
da1 = date(year,month,day)
da2 = date(year,month=1,day=1)
count = (da1-da2).days 1
print(count)
findDays()
# 方式三:比较繁琐的办法:
'''
以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
情况,闰年且输入月份大于3时需考虑多加一天
'''
da = input("请输入日期,格式为XXXX-XX-XX:")
date = da.split('-')
year = int(date[0])
month = int(date[1])
day = int(date[2])
list_month = [31,28,31,30,31,30,31,31,30,31,30,31]
print('计算前:平年闰年:',list_month[1])
count_day = 0
if((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)):
list_month[1] = 29
print('平年闰年:',list_month[1])
for i in range(12):
if month > i 1:
count_day = list_month[i]
else:
count_day = day
break
print("这一天是 {} 年的第 {} 天".format(year,count_day))