本文共 4198 字,大约阅读时间需要 13 分钟。
Python timedelta object is used to perform datetime manipulations in an easy way. The timedelta class is part of .
Python timedelta对象用于以简单的方式执行日期时间操作。 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对象。
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 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 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 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
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)
Python timedelta object is very useful for datetime manipulations. The support for basic arithmetic operators makes it very easy to use.
Python timedelta对象对于日期时间操作非常有用。 基本算术运算符的支持使其非常易于使用。
Reference:
参考:
翻译自:
转载地址:http://xxmzd.baihongyu.com/