Python timedelta
Python timedelta object is used to perform datetime manipulations in an easy way. The timedelta class is part of datetime module.
Python timedelta对象用于以简单的方式执行日期时间操作。 timedelta类是datetime模块的一部分。
Pythoon timedelta (Pythoon timedelta)
Python timedelta object represents a duration of time. We can create its object using following factory method.
Python timedelta对象代表持续时间。 我们可以使用以下工厂方法创建其对象。
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
Note that timedelta() function takes keyword arguments. All arguments are optional and default to 0. Arguments may be integers or floats and may be positive or negative.
请注意,timedelta()函数采用关键字参数。 所有参数都是可选的,默认值为0。参数可以是整数或浮点数,并且可以是正数或负数。
The timedelta object supports mathematical operations such as addition, subtraction, multiplication etc. using basic operators, so it’s very easy to use it. It’s mostly used to get a datetime object with some delta date and time.
timedelta对象使用基本运算符支持数学运算,例如加法,减法,乘法等,因此非常易于使用。 它通常用于获取具有某些增量日期和时间的datetime对象。
Python timedelta示例 (Python timedelta example)
Let’s have a look at some examples of getting future dates and past dates using timedelta object.
让我们看一些使用timedelta对象获取未来日期和过去日期的示例。
from datetime import datetime, timedeltacurrent_datetime = datetime.now()# future dates
one_year_future_date = current_datetime + timedelta(days=365)print('Current Date:', current_datetime)
print('One year from now Date:', one_year_future_date)# past dates
three_days_before_date = current_datetime - timedelta(days=3)
print('Three days before Date:', three_days_before_date)
Output:
输出:
Current Date: 2018-09-18 12:33:30.656394
One year from now Date: 2019-09-18 12:33:30.656394
Three days before Date: 2018-09-15 12:33:30.656394
带有日期和时间的Python timedelta (Python timedelta with date and time)
Python timedelta supports addition and subtraction with date object too.
Python timedelta也支持对date对象进行加减。
dt = current_datetime.date()
print('Current Date:', dt)
dt_tomorrow = dt + timedelta(days=1)
print('Tomorrow Date:', dt_tomorrow)
Output:
输出:
Current Date: 2018-09-18
Tomorrow Date: 2018-09-19
However, timedelta doesn’t support the same operations with time object.
但是,timedelta不支持与time对象相同的操作。
tm = current_datetime.time()
print('Current Time:', tm)
tm_after_30_mins = tm + timedelta(minutes=30)
Above code will produce the following error message.
上面的代码将产生以下错误消息。
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'
Python timedelta属性 (Python timedelta attributes)
Python timedelta class has three attributes.
Python timedelta类具有三个属性。
print(timedelta.max)
print(timedelta.min)
print(timedelta.resolution)
Output:
输出:
999999999 days, 23:59:59.999999
-999999999 days, 0:00:00
0:00:00.000001
Python timedelta总秒数 (Python timedelta total seconds)
Python timedelta object total_seconds() method returns the total number of seconds.
Python timedelta对象total_seconds()方法返回秒总数。
print('Seconds in an year:', timedelta(days=365).total_seconds())
Output: Seconds in an year: 31536000.0
输出: Seconds in an year: 31536000.0
Python timedelta操作 (Python timedelta operations)
Let’s look at some more operations between timedelta objects.
让我们看一下timedelta对象之间的更多操作。
ct = current_datetime + timedelta(seconds=60) - timedelta(seconds=60)
print(current_datetime == ct)ct = current_datetime + timedelta(seconds=10) * 6
print('Current Time:', current_datetime)
print('One Min from Current Time:', ct)print('Timedelta absolute value:', abs(timedelta(days=-10)))
print('Timedelta String Representation:', str(timedelta(days=1, seconds=30, hours=10, milliseconds=300)))
print('Timedelta Object Representation:', repr(timedelta(days=1, seconds=30, hours=10, milliseconds=300)))
Output:
输出:
True
Current Time: 2018-09-18 12:47:28.331197
One Min from Current Time: 2018-09-18 12:48:28.331197
Timedelta absolute value: 10 days, 0:00:00
Timedelta String Representation: 1 day, 10:00:30.300000
Timedelta Object Representation: datetime.timedelta(days=1, seconds=36030, microseconds=300000)
摘要 (Summary)
Python timedelta object is very useful for datetime manipulations. The support for basic arithmetic operators makes it very easy to use.
Python timedelta对象对于日期时间操作非常有用。 基本算术运算符的支持使其非常易于使用。
Reference: Official Documentation
参考: 官方文档
翻译自: https://www.journaldev.com/23334/python-timedelta
Python timedelta相关推荐
- Python timedelta total_seconds()方法与示例
Python timedelta.total_seconds()方法 (Python timedelta.total_seconds() Method) timedelta.timedeltotal_ ...
- python timedelta() 和relativedelta()的区别
python timedelta() 和relativedelta()的区别 在挖掘特征时,往往需要按照时间段来统计特征,例如最近一个月.最近3个月.最近半年.最近一年 某用户的行为数据,那么如何计算 ...
- Python timedelta 日期时间的加减等运算示例
摘自Python3-CookBook 问题一 你需要执行简单的时间转换,比如天到秒,小时到分钟等的转换 解决方案 为了执行不同时间单位的转换和计算,请使用datatime模块.比如,为了表示一个时间段 ...
- python timedelta对象_python timedelta函数是什么?
对于python中的时间获取,我们最近学习的datetime模块可以解决.在这个模块中还有许多函数可以供我们使用,比如时间的差值,就可以选择对应的timedalte函数.这个函数比较特殊,有三种只读属 ...
- python timedelta 求时间差省去繁琐格式转换
目录 一.需求 二.实现 三.补充 一.需求 需要实现一个小功能,求两个时间差,如果大于某个值,就做下一步操作,否则等待. 二.实现 可以使用 datetime.timedelta 1) 将时间转化为 ...
- python timedelta 格式化_格式化python timedelta对象
我比较新的蟒蛇 我有两个datetime对象.我需要计算它们之间的timedelta,然后以spcific格式显示输出. Alpha_TimeObj = datetime.datetime(int(A ...
- python timedelta函数_通过日期字段提取年月日、timedelta提取时分秒并进行小时汇总...
原博地址 知识梳理不易,请尊重劳动成果,文章仅发布在CSDN网站上,在其他网站看到该博文均属于未经作者授权的恶意爬取信息 如若转载,请标明出处,谢谢! 1 业务需求 在进行数据分析,经常会遇到时间处理 ...
- python的datetime举例_Python datetime.timedelta()用法及代码示例
Python timedelta()函数存在于datetime库中,该函数通常用于计算日期差,也可以用于Python中的日期操作.这是执行日期操作的最简单方法之一. 用法: datetime.time ...
- python django 表单_Django-表单处理
这个世界像是蒙着一层白雾,看不清这个世界也看不见自己,只是感觉到脚下还踩着坚实的土地,由此证明自己还活着,除了自己一无所有. 使用表单 以前提交表单内容数据都是在HTML中写一个表单,然后发送到后端, ...
最新文章
- 深度学习debug沉思录!
- Winform开发框架之肖像显示保存控件的实现
- 共享打印机出错,错误代码0x00000bcb解决方法汇总
- [深度学习]实现一个博弈型的AI,从五子棋开始(1)
- Azure SQL Database (1) 用户手册
- css:before和after中的content属性
- 华为超大云数据中心落地贵州,这些硬核技术有利支撑“东数西算”
- Python截屏扩展库pyscreenshot安装与使用
- JSONObject和JSONArray使用
- java websocket注解_【websocket】spring boot 集成 websocket 的四种方式
- module.exports 和 exports 的区别
- python动图自动识别,自动识别图像中的模式
- win7+nfs文件服务器,win7如何挂载nfs服务器
- Unity3D中玩家的移动方式,三大类型,八种方式
- Java8新特性 Stream流常用方法
- 【超实用】在微信文章中点击号码直接拨打电话,轻松运营~
- c++ 计算cpu占用率
- zabbix4.0利用API导入导出dashboard
- 千锋Flask学习笔记
- Unicode编码(统一码)