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


Posted in Python onMarch 26, 2020

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

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

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

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

首先是输入的问题,个人认为分别输入年月份是一件很初级的要求,就实现了形如“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计算指定日期是今年第几天内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python 网络编程起步(Socket发送消息)
Sep 06 Python
Python编程中的反模式实例分析
Dec 08 Python
对python使用telnet实现弱密码登录的方法详解
Jan 26 Python
python 怎样将dataframe中的字符串日期转化为日期的方法
Sep 26 Python
Python实现银行账户资金交易管理系统
Jan 03 Python
在pycharm中实现删除bookmark
Feb 14 Python
Keras:Unet网络实现多类语义分割方式
Jun 11 Python
解决TensorFlow程序无限制占用GPU的方法
Jun 30 Python
详解Tensorflow不同版本要求与CUDA及CUDNN版本对应关系
Aug 04 Python
Python实现迪杰斯特拉算法并生成最短路径的示例代码
Dec 01 Python
使用python操作lmdb对数据读取的实例
Dec 11 Python
Python-OpenCV实现图像缺陷检测的实例
Jun 11 Python
Python函数默认参数常见问题及解决方案
Mar 26 #Python
Python内建序列通用操作6种实现方法
Mar 26 #Python
PyQt5 界面显示无响应的实现
Mar 26 #Python
Python基于class()实现面向对象原理详解
Mar 26 #Python
Python文件读写w+和r+区别解析
Mar 26 #Python
Python装饰器实现方法及应用场景详解
Mar 26 #Python
pycharm中导入模块错误时提示Try to run this command from the system terminal
Mar 26 #Python
You might like
浅析PKI加密解密 OpenSSL
2013/07/01 PHP
swoole_process实现进程池的方法示例
2018/10/29 PHP
PHP字符串与数组处理函数用法小结
2020/01/07 PHP
一份老外写的XMLHttpRequest代码多浏览器支持兼容性
2007/01/11 Javascript
基于jQuery的弹出框插件
2012/03/18 Javascript
从数据结构的角度分析 for each in 比 for in 快的多
2013/07/07 Javascript
AngularJs bootstrap搭载前台框架——基础页面
2016/09/01 Javascript
jQuery ui autocomplete选择列表被Bootstrap模态窗遮挡的完美解决方法
2016/09/23 Javascript
如何开发出更好的JavaScript模块
2017/12/22 Javascript
Vue表单输入绑定的示例代码
2018/11/01 Javascript
angular6 利用 ngContentOutlet 实现组件位置交换(重排)
2018/11/02 Javascript
对vue生命周期的深入理解
2020/12/03 Vue.js
[00:17]天涯墨客一技能展示
2018/08/25 DOTA
[54:53]完美世界DOTA2联赛PWL S2 GXR vs PXG 第二场 11.18
2020/11/18 DOTA
可用于监控 mysql Master Slave 状态的python代码
2013/02/10 Python
Python单例模式实例详解
2017/03/01 Python
利用Python实现原创工具的Logo与Help
2018/12/03 Python
使用Python OpenCV为CNN增加图像样本的实现
2019/06/10 Python
Python 分发包中添加额外文件的方法
2019/08/16 Python
django queryset相加和筛选教程
2020/05/18 Python
Pandas对每个分组应用apply函数的实现
2020/12/13 Python
香蕉共和国加拿大官网:Banana Republic加拿大
2018/08/06 全球购物
Bally澳大利亚官网:瑞士奢侈品牌
2018/11/01 全球购物
西班牙三叶草药房:Farmacias Trébol
2019/05/03 全球购物
巴西电子、家电、智能手机购物网站:Girafa
2019/06/04 全球购物
公司JAVA开发面试题
2015/04/02 面试题
庆八一活动方案
2014/01/25 职场文书
吸烟检讨书2000字
2014/02/13 职场文书
科级干部群众路线教育实践活动对照检查材料思想汇报
2014/09/20 职场文书
2014年安置帮教工作总结
2014/12/11 职场文书
实习协议书
2015/01/27 职场文书
销售经理助理岗位职责
2015/04/13 职场文书
中学语文教学反思
2016/02/16 职场文书
python实现语音常用度量方法的代码详解
2021/05/25 Python
解决Pytorch修改预训练模型时遇到key不匹配的情况
2021/06/05 Python
Nginx的gzip相关介绍
2022/05/11 Servers