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数据结构之Array用法实例
Oct 09 Python
Python访问纯真IP数据库脚本分享
Jun 29 Python
Python QQBot库的QQ聊天机器人
Jun 19 Python
python的sorted用法详解
Jun 25 Python
Python实现FLV视频拼接功能
Jan 21 Python
python_array[0][0]与array[0,0]的区别详解
Feb 18 Python
解决pycharm每次打开项目都需要配置解释器和安装库问题
Feb 26 Python
Python Dict找出value大于某值或key大于某值的所有项方式
Jun 05 Python
python实现模拟器爬取抖音评论数据的示例代码
Jan 06 Python
python使用Windows的wmic命令监控文件运行状况,如有异常发送邮件报警
Jan 30 Python
Django 如何实现文件上传下载
Apr 08 Python
详解Python如何批量采集京东商品数据流程
Jan 22 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
如何正确理解PHP的错误信息
2006/10/09 PHP
如何在WIN2K下安装PHP4.04
2006/10/09 PHP
php ZipArchive压缩函数详解实例
2013/11/06 PHP
ThinkPHP实现将本地文件打包成zip下载
2014/06/26 PHP
利用PHP函数计算中英文字符串长度的方法
2014/11/11 PHP
Yii中CGridView关联表搜索排序方法实例详解
2014/12/03 PHP
Laravel用户授权系统的使用方法示例
2018/09/16 PHP
关于event.cancelBubble和event.stopPropagation()的区别介绍
2011/12/11 Javascript
javascript教程之不完整的继承(js原型链)
2014/01/13 Javascript
兼容主流浏览器的jQuery+CSS 实现遮罩层的简单代码
2014/10/14 Javascript
JQuery标签页效果的两个实例讲解(4)
2015/09/17 Javascript
基于JavaScript实现Json数据根据某个字段进行排序
2015/11/24 Javascript
jQuery Ajax 加载数据时异步显示加载动画
2016/08/01 Javascript
Vue.JS入门教程之处理表单
2016/12/01 Javascript
React全家桶环境搭建过程详解
2018/05/18 Javascript
JS造成内存泄漏的几种情况实例分析
2020/03/02 Javascript
JS浏览器BOM常见操作实例详解
2020/04/27 Javascript
Vue router传递参数并解决刷新页面参数丢失问题
2020/12/02 Vue.js
Django使用httpresponse返回用户头像实例代码
2018/01/26 Python
Python任意字符串转16, 32, 64进制的方法
2019/06/12 Python
python multiprocessing多进程变量共享与加锁的实现
2019/10/02 Python
关于tf.nn.dynamic_rnn返回值详解
2020/01/20 Python
tensorflow 实现自定义梯度反向传播代码
2020/02/10 Python
canvas实现高阶贝塞尔曲线(N阶贝塞尔曲线生成器)
2018/01/10 HTML / CSS
世界第一冲浪品牌:O’Neill
2016/08/30 全球购物
时装界的“朋克之母”:Vivienne Westwood
2017/07/06 全球购物
如何开启linux的ssh服务
2013/06/03 面试题
教师党员思想汇报
2014/01/06 职场文书
物业总经理岗位职责
2014/02/28 职场文书
党的群众路线教育实践活动个人对照检查剖析材料
2014/09/23 职场文书
助学感谢信范文
2015/01/21 职场文书
2015年社区宣传工作总结
2015/05/20 职场文书
反邪教观后感
2015/06/11 职场文书
关于springboot 配置date字段返回时间戳的问题
2021/07/25 Java/Android
CentOS8.4安装Redis6.2.6的详细过程
2021/11/20 Redis
MySQL如何修改字段类型和字段长度
2022/06/10 MySQL