黑松山资源网 Design By www.paidiu.com
本文实例讲述了Python编程实现输入某年某月某日计算出这一天是该年第几天的方法。分享给大家供大家参考,具体如下:
#基于 Python3
一种做法:
def is_leap_year(year): # 判断闰年,是则返回True,否则返回False if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: return True else: return False def function1(year, month, day): # 计算给定日期是那一年的第几天 leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] no_leap_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if is_leap_year(year): result = sum(leap_year[:month - 1]) + day else: result = sum(no_leap_year[:month - 1]) + day return result
但是如果是你自己遇到了这样的需求,那么就没必要这么复杂了。因为Python内置了完善的时间和日期处理函数。
import datetime import time def function2(year, month, day): # 直接使用Python内置模块datetime的格式转换功能得到结果 date = datetime.date(year, month, day) return date.strftime('%j')
需要注意的是,上面的写法里函数的参数分别是年月日的整数,如果你想传入字符串,比如"2016-10-1",那就需要先对字符串做处理了。
同样的,也可以自己做或者用内置函数。
# 假如输入格式为字符串(比如从命令行读入字符串2016-10-1),则需要先对输入内容进行处理 _input = '2016-10-1' _year1 = int(_input.split('-')[0]) _month1 = int(_input.split('-')[1]) _day1 = int(_input.split('-')[2]) # 当然你也可以用datetime的内置方法进行格式处理 t = time.strptime(_input, '%Y-%m-%d') _year2 = t.tm_year _month2 = t.tm_mon _day2 = t.tm_mday
下面是完整的代码,测试"2016-10-1"的结果均为275。
import datetime import time def is_leap_year(year): # 判断闰年,是则返回True,否则返回False if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: return True else: return False def function1(year, month, day): # 计算给定日期是那一年的第几天 leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] no_leap_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if is_leap_year(year): result = sum(leap_year[:month - 1]) + day else: result = sum(no_leap_year[:month - 1]) + day return result def function2(year, month, day): # 直接使用Python内置模块datetime的格式转换功能得到结果 date = datetime.date(year, month, day) return date.strftime('%j') print(function1(2016, 10, 1)) print(function2(2016, 10, 1)) # 假如输入格式为字符串(比如从命令行读入字符串2016-10-1),则需要先对输入内容进行处理 _input = '2016-10-1' _split = _input.split('-') _year1 = int(_split[0]) _month1 = int(_split[1]) _day1 = int(_split[2]) print(function1(_year1, _month1, _day1)) print(function2(_year1, _month1, _day1)) # 当然你也可以用datetime的内置方法进行格式处理 t = time.strptime(_input, '%Y-%m-%d') _year2 = t.tm_year _month2 = t.tm_mon _day2 = t.tm_mday print(function1(_year2, _month2, _day2)) print(function2(_year2, _month2, _day2))
# 后面发现我为了编函数写复杂了,如果输入是字符串其实一句话就好 import time _input = '2016-10-1' # 详见Python日期和字符串格式互相转换 https://www.jb51.net/article/66019.htm t = time.strptime(_input, '%Y-%m-%d') print(time.strftime('%j',t))
PS:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在线万年历日历:
http://tools.jb51.net/bianmin/wannianli
在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python日期与时间操作技巧总结》、《Python URL操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 【原神】V5.0攻略 | 林尼攻略一图流
- 李翊君.1993-相思的烈酒【上华】【WAV+CUE】
- 古巨基.1998-LEO.KU(国)【千禧年代】【WAV+CUE】
- 郭子.2001-原来你什么都不想要创作集丫滚石】【WAV+CUE】
- 《使命召唤:黑色行动6》新预告公布!10月25日发售
- Atlus《暗喻幻想》媒体评分汇总:高分好评如潮!
- 2024金摇杆奖提名揭晓 《黑神话》角逐最佳视觉设计!
- 群星《新说唱2024 第3期 (上)》[320K/MP3][32.76MB]
- 群星《新说唱2024 第3期 (上)》[FLAC/分轨][95.38MB]
- 群星《新说唱2024 第3期 (下)》[320K/MP3][31.36MB]
- 幻兽帕鲁手游什么时候正式上线 最新消息一览
- 西普大陆BOSS位置盘点 解锁天启纪元玩法
- 西普大陆精灵进阶培养攻略 精灵养成指南
- dnf手游法控法系职业哪个强 dnf手游法控法系职业强度排行
- 魔兽世界血藤护目镜图纸在哪买 wlk血藤护目镜图纸购买位置介绍