Python实现计算两个时间之间相差天数的方法


Posted in Python onMay 10, 2017

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

#-*- encoding:UTF-8 -*-
from datetime import date
import time
nowtime = date.today()
def convertstringtodate(stringtime):
  "把字符串类型转换为date类型"
  if stringtime[0:2] == "20":
    year=stringtime[0:4]
    month=stringtime[4:6]
    day=stringtime[6:8]
    begintime=date(int(year),int(month),int(day))
    return begintime
  else :
    year="20"+stringtime[0:2]
    month=stringtime[2:4]
    day=stringtime[4:6]
    begintime=date(int(year),int(month),int(day))
    return begintime
def comparetime(nowtime,stringtime):
  "比较两个时间,并返回两个日期之间相差的天数"
  if isinstance(nowtime,date):
    pass
  else:
    nowtime=convertstringtodate(nowtime)
  if isinstance(stringtime,date):
    pass
  else:
    stringtime=convertstringtodate(stringtime)
  result=nowtime-stringtime
  return result.days
"""
  if stringtime[0:2] == "20":
    year=stringtime[0:4]
    month=stringtime[4:6]
    day=stringtime[6:8]
    begintime=date(int(year),int(month),int(day))
    endtime=nowtime
    result=endtime-begintime
    return result.days
  else :
    year="20"+stringtime[0:2]
    month=stringtime[2:4]
    day=stringtime[4:6]
    begintime=date(int(year),int(month),int(day))
    endtime=nowtime
    result=endtime-begintime
    return result.days
"""
print isinstance("20141012",date)
print comparetime(nowtime,"140619")

PS:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:
http://tools.3water.com/jisuanqi/date_jisuanqi

在线万年历日历:
http://tools.3water.com/bianmin/wannianli

在线阴历/阳历转换工具:
http://tools.3water.com/bianmin/yinli2yangli

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
wxpython 最小化到托盘与欢迎图片的实现方法
Jun 09 Python
Python编程中归并排序算法的实现步骤详解
May 04 Python
Python 搭建Web站点之Web服务器网关接口
Nov 06 Python
Python复制Word内容并使用格式设字体与大小实例代码
Jan 22 Python
Python中的单继承与多继承实例分析
May 10 Python
python框架中flask知识点总结
Aug 17 Python
python 将对象设置为可迭代的两种实现方法
Jan 21 Python
浅谈PYTHON 关于文件的操作
Mar 19 Python
基于Python打造账号共享浏览器功能
May 30 Python
使用 Python ssh 远程登陆服务器的最佳方案
Mar 06 Python
如何用Matlab和Python读取Netcdf文件
Feb 19 Python
使用pandas模块实现数据的标准化操作
May 14 Python
Python开发的实用计算器完整实例
May 10 #Python
Python只用40行代码编写的计算器实例
May 10 #Python
Python实现脚本锁功能(同时只能执行一个脚本)
May 10 #Python
python 3.5下xadmin的使用及修复源码bug
May 10 #Python
Python遍历文件夹和读写文件的实现方法
May 10 #Python
python中requests小技巧
May 10 #Python
Python实现针对中文排序的方法
May 09 #Python
You might like
DOTA2 6.87版本后新眼位详解攻略
2020/04/20 DOTA
了解Joomla 这款来自国外的php网站管理系统
2010/03/11 PHP
php设计模式 Singleton(单例模式)
2011/06/26 PHP
PHP pathinfo()获得文件的路径、名称等信息说明
2011/09/13 PHP
php unset全局变量运用问题的深入解析
2013/06/17 PHP
PHP的foreach中使用引用时需要注意的一个问题和解决方法
2014/05/29 PHP
js中各浏览器中鼠标按键值的差异
2011/04/07 Javascript
js向上无缝滚动,网站公告效果 具体代码
2013/11/18 Javascript
jquery根据属性和index来查找属性值并操作
2014/07/25 Javascript
node.js中的fs.exists方法使用说明
2014/12/17 Javascript
JS实现网页标题栏显示当前时间和日期的完整代码
2015/11/02 Javascript
js阻止默认浏览器行为与冒泡行为的实现代码
2016/05/15 Javascript
js仿淘宝和百度文库的评分功能
2016/05/15 Javascript
使用Promise解决多层异步调用的简单学习心得
2016/05/17 Javascript
Javascript json object 与string 相互转换的简单实现
2016/09/27 Javascript
JavaScript手风琴页面制作
2017/05/17 Javascript
layui问题之模拟table表格中的选中按钮选中事件的方法
2019/09/20 Javascript
Javascript confirm多种使用方法解析
2020/09/25 Javascript
[09:47]2018DOTA2亚洲邀请赛4.5SOLO赛 No[o]ne vs Sumail
2018/04/06 DOTA
Python实现简单字典树的方法
2016/04/29 Python
Python的Twisted框架中使用Deferred对象来管理回调函数
2016/05/25 Python
独特的python循环语句
2016/11/20 Python
对python多线程SSH登录并发脚本详解
2019/02/14 Python
利用Python实现字幕挂载(把字幕文件与视频合并)思路详解
2020/10/21 Python
Css3实现无缝滚动防抖
2020/09/14 HTML / CSS
HTML5标签小集
2011/08/02 HTML / CSS
HTML5自定义data-* data(obj)属性和jquery的data()方法的使用
2012/12/13 HTML / CSS
HTML5 HTMLCollection和NodeList的区别详解
2020/04/29 HTML / CSS
CHARLES & KEITH英国官网:新加坡时尚品牌
2018/07/04 全球购物
师范应届生求职信
2013/11/15 职场文书
我为自己代言广告词
2014/03/18 职场文书
行政部经理助理岗位职责
2014/06/15 职场文书
会计系毕业求职信
2014/08/07 职场文书
2014年学校卫生工作总结
2014/11/20 职场文书
4S店客服专员岗位职责
2015/04/07 职场文书
python+opencv实现目标跟踪过程
2022/06/21 Python