今天是第几天python_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三种方法计算指定日期是今年的第几天的文章就介绍到这了,更多相关python计算指定日期是今年第几天内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

时间: 2020-03-25

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值