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计算指定日期是今年第几天内容请搜索来客网以前的文章或继续浏览下面的相关文章希望大家以后多多支持来客网!