欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python计算两个日期相差天数的方法示例

时间:2021-05-21 08:29:10|栏目:Python代码|点击:

本文实例讲述了Python计算两个日期相差天数的方法。分享给大家供大家参考,具体如下:

#!/usr/bin/python
import time
import sys
def dateinput():
    date = raw_input('please input the first date: ')
    return date
def datetrans(tdate):
    spdate = tdate.replace("/","-")
    try:
        datesec = time.strptime(spdate,'%Y-%m-%d')
    except ValueError:
        print "%s is not a rightful date!!" % tdate
        sys.exit(1)
    return time.mktime(datesec)
def daysdiff(d1,d2):
    daysec = 24 * 60 * 60
    return int(( d1 - d2 )/daysec)
date1 = dateinput()
date2 = dateinput()
date1sec = datetrans(date1)
date2sec = datetrans(date2)
print "The number of days between two dates is: ",daysdiff(date1sec,date2sec)

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程序设计有所帮助。

上一篇:python 数据库查询返回list或tuple实例

栏    目:Python代码

下一篇:python 不关闭控制台的实现方法

本文标题:Python计算两个日期相差天数的方法示例

本文地址:http://www.codeinn.net/misctech/126072.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有