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

(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. 【Leetcode】【Medium】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. javascript 随机数 生成 n-m

    例子:生成800-1500的随机整数,包含800但不包含1500 代码如下: 1500-800 = 700 Math.random()*700 var num = Math.random()*700 ...

  3. iptables:no config file

    防火墙规则默认都是在/etc/sysconfig/iptables这个文件中的 出现这个问题,是因为在/etc/sysconfig/目录下没有找到iptables这个文件 可以使用service ip ...

  4. msvcr100.dll丢失原因及解决方法

    msvcr100.dll为Visual Studio 2010的一个动态链接库,如果某程序是用它开发出来的,那么该程序的运行就有可能需要此动态链接库.有些程序直接将其打包到了安装目录,并注册,就不会出 ...

  5. 学习Road map Part 02 机器学习和图像识别

    方法:结合项目.竞赛.mentor计划

  6. SharpZipLib压缩解压

    一.介绍 SharpZipLib是一个完全由C#编写的ZIP,GZIP,Tar和BZIP2 Library,可以方便的支持这几种格式的压缩和解压缩. https://github.com/icshar ...

  7. Disruptor

    高性能队列Disruptor系列2--浅析Disruptor   目录 1. Disruptor简单介绍2. 为什么Disruptor如此之快3. Disruptor结构分析 1. Disruptor ...

  8. 移动端 html基值(转载)

    (function () { document.addEventListener('DOMContentLoaded', function () { var html = document.docum ...

  9. 弃坑pexpect,入坑paramiko

    上文书说到,ssh库pexpect的使用,简直就是个“月亮公主”——满眼全是坑.勉强把程序写好了,跑起来的时候发现了一个新坑,让我不可抗拒的把它弃掉了——经常莫名其妙的连不上服务器!开线程连接14台服 ...

  10. 苹果手机(ios系统)蓝牙BLE的一些特点

    摘自<BluetoothDesignGuidelines.pdf>文档 1. pairing: 苹果手机无法主动发起SMP配对流程,可通过以下两种方式发起配对流程: (1)从端主动发起配对 ...