python輸入某年某月某日,Python_今天是今年第幾天

 2023-10-04 阅读 45 评论 0

摘要:1、傳統套路import time#今天的日期,年、月、日year, month, day = time.localtime()[:3]#每個月正常的天數python輸入某年某月某日、day_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]#閏年2月多一天if year%400==0 or (year%4=

1、傳統套路

import time

#今天的日期,年、月、日

year, month, day = time.localtime()[:3]

#每個月正常的天數

python輸入某年某月某日、day_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

#閏年2月多一天

if year%400==0 or (year%4==0 and year%100!=0):

? ? day_month[1] = 29

#查看今天是今年的第幾天

if month==1:

字節最新估值2023?? ? print(day)

else:

? ? print(sum(day_month[:month-1])+day)

2、Pythonic

import datetime

today = datetime.date.today()

python菜鳥教程、delta = today - datetime.date(today.year,1,1)+datetime.timedelta(days=1)

print(delta.days)

3、More Pythonic

import datetime

print(datetime.date.today().timetuple().tm_yday)

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/112813.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息