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 相关文章推荐
Python实现动态添加类的属性或成员函数的解决方法
Jul 16 Python
python在windows下实现ping操作并接收返回信息的方法
Mar 20 Python
Python实现的数据结构与算法之队列详解
Apr 22 Python
Python实现连接两个无规则列表后删除重复元素并升序排序的方法
Feb 05 Python
python 查找文件名包含指定字符串的方法
Jun 05 Python
解决pycharm运行时interpreter为空的问题
Oct 29 Python
用python标准库difflib比较两份文件的异同详解
Nov 16 Python
Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析
Apr 27 Python
Python属性和内建属性实例解析
Jan 14 Python
Anconda环境下Vscode安装Python的方法详解
Mar 29 Python
OpenCV+python实现膨胀和腐蚀的示例
Dec 21 Python
Python Matplotlib绘制两个Y轴图像
Apr 13 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
删除及到期域名的查看(抢域名必备哦)
2008/05/14 PHP
php 无限级分类,超级简单的无限级分类,支持输出树状图
2014/06/29 PHP
PHP中strtr字符串替换用法详解
2014/11/26 PHP
PHP中使用curl入门教程
2015/07/02 PHP
JS类中定义原型方法的两种实现的区别
2007/03/08 Javascript
阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)
2007/05/08 Javascript
javascript AutoScroller 函数类
2009/05/29 Javascript
JSON 编辑器实现代码
2009/12/06 Javascript
jQuery EasyUI API 中文文档 - MenuButton菜单按钮使用介绍
2011/10/06 Javascript
浅谈页面装载js及性能分析方法
2014/12/09 Javascript
jquery读取xml文件实现省市县三级联动的方法
2015/05/29 Javascript
Bootstrap~多级导航(级联导航)的实现效果【附代码】
2016/03/08 Javascript
Jquery实现$.fn.extend和$.extend函数
2016/04/14 Javascript
angular bootstrap timepicker TypeError提示怎么办
2017/06/13 Javascript
vue input输入框模糊查询的示例代码
2018/05/22 Javascript
JS+HTML5 Canvas实现简单的写字板功能示例
2018/08/30 Javascript
在node中使用jwt签发与验证token的方法
2019/04/03 Javascript
更优雅的微信小程序骨架屏实现详解
2019/08/07 Javascript
layui 上传文件_批量导入数据UI的方法
2019/09/23 Javascript
[00:27]DOTA2战队VP、Secret贺新春
2018/02/11 DOTA
Python运算符重载详解及实例代码
2017/03/07 Python
Python 删除整个文本中的空格,并实现按行显示
2018/07/24 Python
CentOS 7 安装python3.7.1的方法及注意事项
2018/11/01 Python
Python如何实现动态数组
2019/11/02 Python
python 解决flask uwsgi 获取不到全局变量的问题
2019/12/22 Python
英国最好的包装供应商:Priory Direct
2019/12/17 全球购物
Nike瑞士官网:Nike CH
2021/01/18 全球购物
教师考察材料范文
2014/06/03 职场文书
大二学生学年自我鉴定
2014/09/12 职场文书
甜品蛋糕店创业计划书
2014/09/21 职场文书
英语辞职信范文
2015/02/28 职场文书
售房协议书范本
2015/08/11 职场文书
教师教育教学随笔
2015/08/15 职场文书
2019年新郎保证书3篇
2019/10/17 职场文书
基于Go语言构建RESTful API服务
2021/07/25 Golang
vue实现移动端div拖动效果
2022/03/03 Vue.js