计算时间差时,注意天数差引发的问题,获取天数差为

(date2-date1).days

此处,需谨记date2>date1,以保证结果的正确性

具体应用如下:

 # -*-coding:utf8-*-
# 计算时间差
import time
import datetime
def time_interval():
# 获取当前时间
#time_old = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
time_old = '2018-1-1 23:40:01'
print time_old
#time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
time_now = '2018-1-2 00:10:01'
print time_now
old_time = time.strptime(time_old,'%Y-%m-%d %H:%M:%S')
now_time = time.strptime(time_now,'%Y-%m-%d %H:%M:%S')
#print old_time
date1 = datetime.datetime(old_time[0], old_time[1], old_time[2], old_time[3], old_time[4], old_time[5])
date2 = datetime.datetime(now_time[0], now_time[1], now_time[2], now_time[3], now_time[4], now_time[5])
return (date2-date1).seconds print time_interval()

注意:strftime()与strptime()的不同

python应用:日期时间的更多相关文章

  1. Python学习---日期时间

    在Python里面日期时间的功能主要由几个模块提供:time,calendar,datetime,date等 time主要用到的功能函数: #!/usr/bin/python3 # coding:ut ...

  2. Python实用日期时间处理方法汇总

    这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...

  3. 程序员常用6 个 Python 的日期时间库

    内建的 datetime 模块 在跳转到其他库之前,让我们回顾一下如何使用 datetime 模块将日期字符串转换为 Python datetime 对象. 假设我们从 API 接受到一个日期字符串, ...

  4. Python基础 | 日期时间操作

    目录 获取时间 时间映射 格式转换 字符串转日期 日期转字符串 unixtime 时间计算 时间偏移 时间差 "日期时间数据"作为三大基础数据类型之一,在数据分析中会经常遇到. 本 ...

  5. Python数值日期时间笔记

    数值: 格式化 小数位的处理 随机数: random.choice() 序列中随机选择一个值 random.sample() 获取指定数目的序列 random.shuffle() 打乱顺序 rando ...

  6. [ Python入门教程 ] Python中日期时间datetime模块使用实例

    Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...

  7. python 之日期时间处理

    ##python时间操作一般使用time.datetime两个模块 对于time模块,时间的表示模式有3种1.时间戳:time.time()2.字符串: time.strftime('%Y%m%d') ...

  8. Python中日期时间案例演示

    案例:准备10个人姓名,然后为这10个人随机生成生日[都是90后] 1.统计出那些人是夏季[6月-8月]出生的. 2.最大的比最小的大多少天 3.谁的生日最早,谁的生日最晚 备注:春季[3-5]夏季[ ...

  9. python输出日期时间

    import datetime base = datetime.datetime.today() , ): print(base + datetime.timedelta(days=x))

  10. Python中对时间日期的处理方法简单汇总

    这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...

随机推荐

  1. 深入解析Backbone.js框架的依赖库Underscore.js的作用

    这篇文章主要介绍了深入解析Backbone.js框架的依赖库Underscore.js的作用,用过Node.js的朋友对Underscore一定不会陌生:)需要的朋友可以参考下 backbone必须依 ...

  2. 41. First Missing Positive (sort) O(n) time

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  3. MySQL闪回-binlog2sql

    功能 提取SQL 生成回滚SQL     限制: mysql server必须开启,离线模式下不能解析binlog. binlog格式必须是row模式. flashback模式只支持DML,DDL将不 ...

  4. 记两个std接口equal_range,set_difference

    1.equal_range equal_range是C++ STL中的一种二分查找的算法,试图在已排序的[first,last)中寻找value,它返回一对迭代器i和j,其中i是在不破坏次序的前提下, ...

  5. MATLAB基础指令操作

    由于课程实验需要学习使用了MATLAB,在此记录一下MATLAB的基本操作和命令,供参考与查阅. 学习过程中的资料也链接如下: MATLAB矩阵运算:https://wenku.baidu.com/v ...

  6. Codeforces Round #430 (Div. 2) 【A、B、C、D题】

    [感谢牛老板对D题的指点OTZ] codeforces 842 A. Kirill And The Game[暴力] 给定a的范围[l,r],b的范围[x,y],问是否存在a/b等于k.直接暴力判断即 ...

  7. Codeforces Round #423 (Div. 2)

    codeforces 423 A. Restaurant Tables [水题] //注意,一个人选座位的顺序,先去单人桌,没有则去空的双人桌,再没有则去有一个人坐着的双人桌.读清题意. #inclu ...

  8. 十.mysqld_multi stop无效问题

    今天在尝试运行mysqld_report stop的时候,发现无法停止mysql,日志中的错误如下 Stopping MySQL servers mysqladmin: [Warning] Using ...

  9. UVa 1442 - Cave

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  10. [转]删除MSSQL所有的约束及表格

    --删除所有约束.表.视图等SQL脚本 --############################################### --删除所有外键约束 --################# ...