博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python timedelta
阅读量:2545 次
发布时间:2019-05-11

本文共 4198 字,大约阅读时间需要 13 分钟。

Python timedelta object is used to perform datetime manipulations in an easy way. The timedelta class is part of .

Python timedelta对象用于以简单的方式执行日期时间操作。 timedelta类是一部分。

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 datesone_year_future_date = current_datetime + timedelta(days=365)print('Current Date:', current_datetime)print('One year from now Date:', one_year_future_date)# past datesthree_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.656394One year from now Date: 2019-09-18 12:33:30.656394Three 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-18Tomorrow 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:000: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) * 6print('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:

输出:

TrueCurrent Time: 2018-09-18 12:47:28.331197One Min from Current Time: 2018-09-18 12:48:28.331197Timedelta absolute value: 10 days, 0:00:00Timedelta String Representation: 1 day, 10:00:30.300000Timedelta 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对象对于日期时间操作非常有用。 基本算术运算符的支持使其非常易于使用。

. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

转载地址:http://xxmzd.baihongyu.com/

你可能感兴趣的文章
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>