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字符串、元组、列表、字典互相转换的方法
Jan 23 Python
Python正确重载运算符的方法示例详解
Aug 27 Python
Python中pow()和math.pow()函数用法示例
Feb 11 Python
Python正则表达式实现简易计算器功能示例
May 07 Python
python操作日志的封装方法(两种方法)
May 23 Python
Pyqt清空某一个QTreeewidgetItem下的所有分支方法
Jun 17 Python
基于Python新建用户并产生随机密码过程解析
Oct 08 Python
如何基于python实现脚本加密
Dec 28 Python
python实现横向拼接图片
Mar 23 Python
详解用Python爬虫获取百度企业信用中企业基本信息
Jul 02 Python
Python获取android设备cpu和内存占用情况
Nov 15 Python
Python可视化神器pyecharts之绘制箱形图
Jul 07 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文件上传后端处理小技巧
2016/05/22 PHP
php写一个函数,实现扫描并打印出自定目录下(含子目录)所有jpg文件名
2017/05/26 PHP
PHP面向对象之事务脚本模式(详解)
2017/06/07 PHP
如何用ajax来创建一个XMLHttpRequest对象
2012/12/10 Javascript
javaScript如何处理从java后台返回的list
2014/04/24 Javascript
Bootstrap图片轮播组件Carousel使用方法详解
2016/10/20 Javascript
浅述节点的创建及常见功能的实现
2016/12/15 Javascript
jQuery基本选择器和层次选择器学习使用
2017/02/27 Javascript
JS对象深度克隆实例分析
2017/03/16 Javascript
红黑树的插入详解及Javascript实现方法示例
2018/03/26 Javascript
JS计算斐波拉切代码实例
2019/09/12 Javascript
vue+element table表格实现动态列筛选的示例代码
2021/01/14 Vue.js
[41:20]2014 DOTA2华西杯精英邀请赛 5 24 NewBee VS DK
2014/05/26 DOTA
[02:09]抵达西雅图!中国军团加油!
2014/07/07 DOTA
python聊天程序实例代码分享
2013/11/18 Python
Python卸载模块的方法汇总
2016/06/07 Python
python利用正则表达式搜索单词示例代码
2017/09/24 Python
python 实现数组list 添加、修改、删除的方法
2018/04/04 Python
利用PyCharm Profile分析异步爬虫效率详解
2019/05/08 Python
mac安装python3后使用pip和pip3的区别说明
2020/09/01 Python
python excel和yaml文件的读取封装
2021/01/12 Python
美国知名的家庭连锁百货商店:Boscov’s
2017/07/27 全球购物
欧舒丹俄罗斯官方网站:L’OCCITANE俄罗斯
2019/11/22 全球购物
什么是反射
2012/03/17 面试题
单位在职证明范本
2014/01/09 职场文书
大学毕业感言
2014/01/10 职场文书
数控专业个人求职信范文
2014/02/05 职场文书
租房协议书
2014/09/12 职场文书
2014年网络管理员工作总结
2014/12/01 职场文书
5.12护士节活动总结
2015/02/10 职场文书
2015年秋季灭鼠工作总结
2015/07/27 职场文书
公司考勤管理制度
2015/08/04 职场文书
竞聘演讲报告:基本写作有哪些?附开头范文
2019/10/16 职场文书
新学期新寄语,献给新生们!
2019/11/15 职场文书
python全面解析接口返回数据
2022/02/12 Python
spring boot实现文件上传
2022/08/14 Java/Android