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, timedelta

current_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对象对于日期时间操作非常有用。 基本算术运算符的支持使其非常易于使用。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23334/python-timedelta

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值