前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python计算指定日期是今年的第几天(三种方法)

Python计算指定日期是今年的第几天(三种方法)

作者头像
砸漏
发布2020-11-04 16:02:53
1.5K0
发布2020-11-04 16:02:53
举报
文章被收录于专栏:恩蓝脚本

今天早上和腾讯面试官进行了视频面试,由于音量和网络以及我的垃圾电脑的原因,个人感觉黄了…

最后面试官给了我一道简单的计算题:指定日期是今年的第几年

由于电脑卡到打字都打不动,我勉勉强强写了一点,虽然面试官知道了我的想法也了解我的设备情况,最后没让我写完

但是心里惭愧还是时候补齐了…话不多说回到主题吧

首先是输入的问题,个人认为分别输入年月份是一件很初级的要求,就实现了形如“2020-3-26”的字符串解析的两种方法,代码如下:

代码语言:javascript
复制
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]

然后判断是否闰年

代码语言:javascript
复制
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

主函数

代码语言:javascript
复制
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()

以下是全部代码:

代码语言:javascript
复制
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计算指定日期是今年第几天内容请搜索ZaLou.Cn以前的文章或继续浏览下面的相关文章希望大家以后多多支持ZaLou.Cn!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档