编程语言
首页 > 编程语言> > Python 计算今天(某一日期)是今年中的第几天

Python 计算今天(某一日期)是今年中的第几天

作者:互联网

from datetime import datetime

cur_date = datetime.now().strftime('%Y-%m-%d').split('-')
# cur_date = input('请输入要计算的日期,例:2021-3-5:').split('-')
print(cur_date)  # 2021-3-5
cur_year = int(cur_date[0])
cur_month = int(cur_date[1])
cur_day = int(cur_date[2])

days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if cur_year % 400 == 0 or (cur_year % 4 == 0 and cur_year % 100 != 0):
    days[1] = 29
if cur_month == 1:
    days = cur_day
else:
    days = sum(days[0:cur_month - 1]) + cur_day

print(days)

 

标签:cur,第几天,Python,31,30,days,某一,year,date
来源: https://www.cnblogs.com/Small-lucky/p/14307656.html