转换。
(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

最新文章

  1. 【JS】heatmap.js v1.0 到 v2.0,详细总结一下:)
  2. PNG的使用技巧
  3. thinkphp succes error跳转模板 设置
  4. PDO(数据访问抽象层)
  5. Codeforces Round #313 (Div. 2) D. Equivalent Strings
  6. jquery 中如何将数组转化为json字符串,然后再转化回来?
  7. springMVC+Hibernate常用的配置文件
  8. easyui源码翻译1.32--TreeGrid(树形表格)
  9. How to new a screen in linux
  10. 【转】qtp-learn
  11. promise异步编程的原理
  12. Redis学习笔记(1)-安装Oracle VM VirtualBox
  13. [BZOJ1776][Usaco2010 Hol]cowpol 奶牛政坛
  14. Codeforces 918D - MADMAX
  15. PAT 甲级 1113 Integer Set Partition
  16. JavaScript高级 面向对象(9)--深拷贝代码实现
  17. Spring Security 自定义配置(1)
  18. 打开office提示还有几天过期的处理办法
  19. c# mysql and sqlserver数据库连接字符串
  20. Linux入门之运维(1) 系统监控 vmstat top

热门文章

  1. MySQL学习笔记——ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  2. Java 生成验证码
  3. SQL Server错误与事务处理
  4. vue.js入门(3)——组件通信
  5. 并发工具类:CountDownLatch、CyclicBarrier、Semaphore
  6. textarea 换行操作
  7. 执行robot framework 的测试用例 命令行pybot使用方式
  8. MVC中的JS和CSS压缩
  9. [python]用Python进行SQLite数据库操作
  10. Main.C时钟设定