Python时间差中seconds和total_seconds的区别
来源:互联网 发布:em算法例子 编辑:程序博客网 时间:2024/06/08 00:45
在python中经常会用到计算两个时间差,两个日期类型进行相减可以获取到时间差。
经常会使用seconds来获取,其实seconds获取的是仅仅是时间差的秒数,忽略微秒数,忽略天数。
total_seconds()是获取两个时间之间的总差。
total_seconds() = (td.microseconds+ (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 # td是timedelta对象
#-*- coding: UTF-8 -*-'''Created on 2017年10月6日@author: yuthzi'''import datetimet1 = datetime.datetime.strptime("2017-9-06 10:30:00", "%Y-%m-%d %H:%M:%S")t2 = datetime.datetime.strptime("2017-9-06 12:30:00", "%Y-%m-%d %H:%M:%S")interval_time = (t2 - t1).seconds # 输入的结果:7200total_interval_time = (t2 - t1).total_seconds() # 输出结果是: 7200.0print interval_timeprint total_interval_time# 换一个方式t1 = datetime.datetime.strptime("2017-9-06 10:30:00", "%Y-%m-%d %H:%M:%S")t2 = datetime.datetime.strptime("2017-9-08 12:30:00", "%Y-%m-%d %H:%M:%S")interval_time = (t2 - t1).seconds # 输入的结果:7200total_interval_time = (t2 - t1).total_seconds() # 输出结果是: 180000.0print interval_timeprint total_interval_timetd = (t2 - t1)print((td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6)# datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])td = datetime.timedelta(6, 5, 1, 800, 12, 3) print td # 6 days, 3:12:05.800001print td.seconds # 11525 忽略微秒和天print td.total_seconds() # 529925.800001
ref:
https://docs.python.org/2/library/datetime.html?#timedelta-objects
阅读全文
0 0
- Python时间差中seconds和total_seconds的区别
- python2.7使用TimeDelta中total_seconds()方法的问题
- python2.7使用TimeDelta中total_seconds()方法的问题
- python 时间差的计算
- python 中如何计算时间差...
- python 中如何计算时间差...
- Python中Range和XRange的区别
- Python中Range和XRange的区别
- Python中Range和XRange的区别
- Python中Range和XRange的区别
- python中range 和 xrange的区别
- Python中range和xrange的区别
- python中input和raw_input的区别
- python 中 range 和 xrange 的区别
- python 中,match和search的区别
- python中extend和append的区别
- python中%r和%s的区别
- python中input()和raw_input()的区别
- [高级软件工程实验]内部模块化的命令行菜单小程序V2.0
- C 语言的浮点型问题
- Python Basics: Working with Strings
- PyQt5系列学习笔记之01:通过Qt Creator创建.ui文件,pyuic5翻译.ui文件为.py文件,在PyCharm中调用.py文件
- 承德算命大师解读命与运的奥秘,承德算命大师预测指导
- Python时间差中seconds和total_seconds的区别
- 图像增强技术基础、评价指标及其经典算法
- MyBatis框架理解与使用
- 手游服务端框架之使用Redis实现跨服排行榜
- 【JZOJ5391】卡常题
- 求序列的最大最小值
- 多元回归分析(multiple regression)及其应用
- 数据存储---GreenPlum实践
- 顺时针打印矩阵