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 相关文章推荐
使用PDB简单调试Python程序简明指南
Apr 25 Python
Python自动化运维和部署项目工具Fabric使用实例
Sep 18 Python
Python爬虫实现网页信息抓取功能示例【URL与正则模块】
May 18 Python
python使用生成器实现可迭代对象
Mar 20 Python
python去除扩展名的实例讲解
Apr 23 Python
python 请求服务器的实现代码(http请求和https请求)
May 25 Python
python 输出所有大小写字母的方法
Jan 02 Python
opencv3/Python 稠密光流calcOpticalFlowFarneback详解
Dec 11 Python
Python爬虫入门有哪些基础知识点
Jun 02 Python
Python中Selenium模块的使用详解
Oct 09 Python
如何理解及使用Python闭包
Jun 01 Python
Python实现单例模式的5种方法
Jun 15 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跳出循环的方法以及continue、break、exit的区别介绍
2013/07/01 PHP
详解Yii2 rules 的验证规则
2016/12/02 PHP
Yii2使用$this->context获取当前的Module、Controller(控制器)、Action等
2017/03/29 PHP
Dojo之路:如何利用Dojo实现Drag and Drop效果
2007/04/10 Javascript
基于jquery的让页面控件不可用的实现代码
2010/04/27 Javascript
在WordPress中加入Google搜索功能的简单步骤讲解
2016/01/04 Javascript
Nodejs抓取html页面内容(推荐)
2016/08/11 NodeJs
JavaScript中String对象的方法介绍
2017/01/04 Javascript
超级简易的JS计算器实例讲解(实现加减乘除)
2017/08/08 Javascript
AngularJS通过ng-Img-Crop实现头像截取的示例
2017/08/17 Javascript
RequireJS用法简单示例
2018/08/20 Javascript
nodejs中函数的调用实例详解
2018/10/31 NodeJs
webpack 从指定入口文件中提取公共文件的方法
2018/11/13 Javascript
JavaScript惰性求值的一种实现方法示例
2019/01/11 Javascript
微信小程序仿通讯录功能
2020/04/09 Javascript
vue离开当前页面触发的函数代码
2020/09/01 Javascript
[52:52]DOTA2上海特级锦标赛C组资格赛#1 OG VS LGD第三局
2016/02/27 DOTA
详解python中executemany和序列的使用方法
2017/08/12 Python
python算的上脚本语言吗
2020/06/22 Python
Tech21美国/加拿大:英国NO.1防摔保护壳品牌
2018/01/20 全球购物
CHARLES & KEITH台湾官网:新加坡时尚品牌
2019/07/30 全球购物
您在慕尼黑的跑步商店:Lauf-bar
2019/10/11 全球购物
XML文档面试题
2015/08/05 面试题
在Ajax应用中信息是如何在浏览器和服务器之间传递的
2016/05/31 面试题
公司财务总监岗位职责
2013/12/14 职场文书
高中军训广播稿
2014/01/14 职场文书
工作失职检讨书范文
2014/01/16 职场文书
会务接待方案
2014/02/27 职场文书
12岁生日演讲稿
2014/05/14 职场文书
中秋晚会策划方案
2014/06/12 职场文书
2014年调度员工作总结
2014/11/19 职场文书
上课睡觉万能检讨书
2015/02/17 职场文书
MySQL 角色(role)功能介绍
2021/04/24 MySQL
JS Object构造函数之Object.freeze
2021/04/28 Javascript
【DOTA2】半决赛强强对话~ PSG LGD vs EHOME - DPC 2022 CN REGIONAL FINALS WINTER
2022/04/02 DOTA
vue实现在data里引入相对路径
2022/06/05 Vue.js