datetime.timedelta
时间: 2023-11-10 12:06:36 浏览: 99
python时间日期函数与利用pandas进行时间序列处理详解
5星 · 资源好评率100%
The `timedelta` class in Python represents a duration of time, such as a difference between two dates or times. It is used to perform arithmetic operations on dates and times.
A `timedelta` object is created using the `datetime.timedelta()` constructor, which takes arguments such as days, hours, minutes, seconds, microseconds, and milliseconds.
For example:
```python
import datetime
# create a timedelta object representing 1 day
one_day = datetime.timedelta(days=1)
# create a timedelta object representing 2 hours
two_hours = datetime.timedelta(hours=2)
# add the two timedelta objects together
total_time = one_day + two_hours
# print the total time in seconds
print(total_time.total_seconds())
```
Output:
```
93600.0
```
In this example, we created two `timedelta` objects representing one day and two hours, and then added them together to get a total time of 26 hours. We then printed the total time in seconds using the `total_seconds()` method.
阅读全文