python核心编程第六章练习6-15
2024-10-12 01:11:40
转换。
(a)给出两个可识别格式的日期,比如MM/DD/YY或者DD/MM/YY格式。计算出两个日期之间的天数。
(b)给出一个人的生日,计算此人从出生到现在的天数,包括所有的闰月。
(c)还是上面的例子,计算出此人下次过生日还有多少天。
【答案】
(a)代码如下:
def date_convert(date_input):
month = int(date_input.split('/')[0])
day = int(date_input.split('/')[1])
year = int('20' + date_input.split('/')[2])
return (year, month, day) import datetime
date_input = raw_input('Please input the begin date, MM / DD / YY ... ')
d1 = datetime.date(date_convert(date_input)[0], date_convert(date_input)[1], date_convert(date_input)[2])
date_input = raw_input('Please input the end date, MM / DD / YY ... ')
d2 = datetime.date(date_convert(date_input)[0], date_convert(date_input)[1], date_convert(date_input)[2])
print (d2 - d1).days
(b)代码如下:
def date_convert(date_input):
month = int(date_input.split('/')[0])
day = int(date_input.split('/')[1])
year = int(date_input.split('/')[2])
return (year, month, day) import datetime
date_input = raw_input('Please input his birthday, MM / DD / YYYY ... ')
d1 = datetime.date(date_convert(date_input)[0], date_convert(date_input)[1], date_convert(date_input)[2])
print (datetime.date.today() - d1).days
(c)代码如下:
def date_convert(date_input):
month = int(date_input.split('/')[0])
day = int(date_input.split('/')[1])
year = int(date_input.split('/')[2])
return (year, month, day) import datetime
date_input = raw_input('Please input his birthday, MM / DD / YYYY ... ') import time
next_year = int(time.strftime('%Y',time.localtime(time.time()))) + 1
next_birthday = datetime.date(next_year, date_convert(date_input)[1], date_convert(date_input)[2])
print (next_birthday - datetime.date.today()).days
以上代码支持06/13/15 不支持06/13/2015
最新文章
- 【JS】heatmap.js v1.0 到 v2.0,详细总结一下:)
- PNG的使用技巧
- thinkphp succes error跳转模板 设置
- PDO(数据访问抽象层)
- Codeforces Round #313 (Div. 2) D. Equivalent Strings
- jquery 中如何将数组转化为json字符串,然后再转化回来?
- springMVC+Hibernate常用的配置文件
- easyui源码翻译1.32--TreeGrid(树形表格)
- How to new a screen in linux
- 【转】qtp-learn
- promise异步编程的原理
- Redis学习笔记(1)-安装Oracle VM VirtualBox
- [BZOJ1776][Usaco2010 Hol]cowpol 奶牛政坛
- Codeforces 918D - MADMAX
- PAT 甲级 1113 Integer Set Partition
- JavaScript高级 面向对象(9)--深拷贝代码实现
- Spring Security 自定义配置(1)
- 打开office提示还有几天过期的处理办法
- c# mysql and sqlserver数据库连接字符串
- Linux入门之运维(1) 系统监控 vmstat top
热门文章
- MySQL学习笔记——ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- Java 生成验证码
- SQL Server错误与事务处理
- vue.js入门(3)——组件通信
- 并发工具类:CountDownLatch、CyclicBarrier、Semaphore
- textarea 换行操作
- 执行robot framework 的测试用例 命令行pybot使用方式
- MVC中的JS和CSS压缩
- [python]用Python进行SQLite数据库操作
- Main.C时钟设定