这篇文章给大家分享的是有关Python怎么计算指定日期是今年的第几天的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
首先是输入的问题,个人认为分别输入年月份是一件很初级的要求,就实现了形如“2020-3-26”的字符串解析的两种方法,代码如下:
def cal_date_str_spilt(date):
''''
处理形如"2020-3-26"
使用字符串的spilt方法解析
'''
_year = int(date.split('-')[0])
_month = int(date.split('-')[1])
_day = int(date.split('-')[2])
return [_year, _month, _day]
def cal_date_str_time(date):
'''
使用time库内置函数strptime(string, format) return struct_time对象
传入参数:字符串 + 处理格式
'''
_date = time.strptime(date, '%Y-%m-%d')
_year = _date.tm_year
_month = _date.tm_mon
_day = _date.tm_mday
return [_year, _month, _day]
然后判断是否闰年
def judge_leap_year(year, month):
# 只有闰年且月份大于2月才加多一天
if year % 400 == 0 or year % 100 and year % 4 == 0 and month > 2:
return 1
else:
return 0
主函数
def main():
date = input("请输入日期,以'-'分隔:")
sum_1, sum_2 = 0, 0
date_list_1 = cal_date_str_spilt(date)
date_list_2 = cal_date_str_time(date)
month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month_day_lep = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
sum_1 += sum(month_day[:date_list_1[1] - 1]) + date_list_1[2] + judge_leap_year(date_list_1[0], date_list_1[1])
sum_2 += sum(month_day[:date_list_2[1] - 1]) + date_list_2[2] + judge_leap_year(date_list_2[0], date_list_2[1])
print('今天是今年的第' + str(sum_1) + '天')
print('今天是今年的第' + str(sum_2) + '天')
'''
这一段是使用了datetime库的方法,python本身就有处理该类问题的方法
'''
_sum = datetime.date(date_list_1[0], date_list_1[1], date_list_1[2])
sum_3 = _sum.strftime('%j')
if sum_3[0] == '0' and sum_3[1] == '0':
print('今天是今年的第' + str(sum_3[-1:]) + '天')
elif sum_3[0] == '0':
print('今天是今年的第' + str(sum_3[-2:]) + '天')
else:
print('今天是今年的第' + str(sum_3) + '天')
if __name__ == '__main__':
main()
以下是全部代码:
import datetime
import time
def cal_date_str_spilt(date):
''''
处理形如"2020-3-26"
使用字符串的spilt方法解析
'''
_year = int(date.split('-')[0])
_month = int(date.split('-')[1])
_day = int(date.split('-')[2])
return [_year, _month, _day]
def cal_date_str_time(date):
'''
使用time库内置函数strptime(string, format) return struct_time对象
传入参数:字符串 + 处理格式
'''
_date = time.strptime(date, '%Y-%m-%d')
_year = _date.tm_year
_month = _date.tm_mon
_day = _date.tm_mday
return [_year, _month, _day]
def judge_leap_year(year, month):
# 只有闰年且月份大于2月才加多一天
if year % 400 == 0 or year % 100 and year % 4 == 0 and month > 2:
return 1
else:
return 0
def main():
date = input("请输入日期,以'-'分隔:")
sum_1, sum_2 = 0, 0
date_list_1 = cal_date_str_spilt(date)
date_list_2 = cal_date_str_time(date)
month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month_day_lep = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
sum_1 += sum(month_day[:date_list_1[1] - 1]) + date_list_1[2] + judge_leap_year(date_list_1[0], date_list_1[1])
sum_2 += sum(month_day[:date_list_2[1] - 1]) + date_list_2[2] + judge_leap_year(date_list_2[0], date_list_2[1])
print('今天是今年的第' + str(sum_1) + '天')
print('今天是今年的第' + str(sum_2) + '天')
'''
这一段是使用了datetime库的方法,python本身就有处理该类问题的方法
'''
_sum = datetime.date(date_list_1[0], date_list_1[1], date_list_1[2])
sum_3 = _sum.strftime('%j')
if sum_3[0] == '0' and sum_3[1] == '0':
print('今天是今年的第' + str(sum_3[-1:]) + '天')
elif sum_3[0] == '0':
print('今天是今年的第' + str(sum_3[-2:]) + '天')
else:
print('今天是今年的第' + str(sum_3) + '天')
if __name__ == '__main__':
main()
感谢各位的阅读!关于“Python怎么计算指定日期是今年的第几天”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。