Python DateTime - Timedelta 类
Timedelta类用于计算日期之间的差异并表示持续时间。这种差异既可以是正的,也可以是负的。
句法:
class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
例子:
Python3
# Timedelta function demonstration
from datetime import datetime, timedelta
# creating dateime objects
date1 = datetime(2020, 1, 3)
date2 = datetime(2020, 2, 3)
# difference between dates
diff = date2 - date1
print("Difference in dates:", diff)
# Adding days to date1
date1 += timedelta(days = 4)
print("Date1 after 4 days:", date1)
# Sutracting days from date1
date1 -= timedelta(15)
print("Date1 before 15 days:", date1)
Python3
from datetime import timedelta
# Getting minimum value
Min = timedelta.min
print("Minimum value of timedelta object", Min)
# Getting minimum value
Max = timedelta.max
print("Maximum value of timedelta object", Max)
Python3
from datetime import timedelta
# Getting minimum value
obj = timedelta(hours=1)
print(obj.total_seconds())
obj = timedelta(minutes=1)
print(obj.total_seconds())
obj = timedelta(days=1)
print(obj.total_seconds())
Python3
from datetime import timedelta
# creating the timedelta object
t1 = timedelta(days=1)
print("Original timedelta:", t1)
# multiplication
t2 = t1*5.5
print("After Multiplication:", t2)
# Subtraction
res = t2 - t1
print("After Subtraction:", res)
# addition
res += t2
print("After Addition:", res)
# division
res = t2/2.5
print("After division:", res)
# floor division
res = t2 //2
print("After floor division:", res)
# Modulo
res = t2%timedelta(days=3)
print("After Modulo:", res)
Python3
from datetime import timedelta
# creating the timedelta object
t1 = timedelta(days=1)
print("Original timedelta:", t1)
# Negatiob of timedelta object
t1 = -(t1)
print("After Negation:", t1)
# Getting Absolute value
t1 = abs(t1)
print("Absolute Value:", t1)
# Getting string representation
print("String representation:", str(t1))
# Getting Constructor call
print("Constructor call:", repr(t1))
Difference in dates: 31 days, 0:00:00
Date1 after 4 days: 2020-01-07 00:00:00
Date1 before 15 days: 2019-12-23 00:00:00
类属性:
让我们看看这个类提供的属性——Attribute Name Description min minimum value of timedelta object is -999999999 max maximum value of timedelta object is 999999999 resolution The minimum possible difference between timedelta objects
示例:获取 timedelta 对象的最小值和最大值
蟒蛇3
from datetime import timedelta
# Getting minimum value
Min = timedelta.min
print("Minimum value of timedelta object", Min)
# Getting minimum value
Max = timedelta.max
print("Maximum value of timedelta object", Max)
Minimum value of timedelta object -999999999 days, 0:00:00
Maximum value of timedelta object 999999999 days, 23:59:59.999999
输出
Minimum value of timedelta object -999999999 days, 0:00:00
Maximum value of timedelta object 999999999 days, 23:59:59.999999
类函数
Timedelta 类仅提供一个函数,即total_seconds()。此方法返回 timedelta 对象提供的持续时间(以秒为单位)。
注意:对于超过 270 年的持续时间,此方法将精确到微秒。
示例:以秒为单位获取各种持续时间
蟒蛇3
from datetime import timedelta
# Getting minimum value
obj = timedelta(hours=1)
print(obj.total_seconds())
obj = timedelta(minutes=1)
print(obj.total_seconds())
obj = timedelta(days=1)
print(obj.total_seconds())
3600.0
60.0
86400.0
Timedelta 类支持的操作
Operator | Description |
---|---|
Addition (+) | Adds and returns two timedelta objects |
Subtraction (-) | Subtracts and returns two timedelta objects |
Multiplication (*) | Multiplies timedelta object with float or int |
Division (/) | Divides the timedelta object with float or int |
Floor division (//) | Divides the timedelta object with float or int and return the int of floor value of the output |
Modulo (%) | Divides two timedelta object and returns the remainder |
+(timedelta) | Returns the same timedelta object |
-(timedelta) | Returns the resultant of -1*timedelta |
abs(timedelta) | Returns the +(timedelta) if timedelta.days > 1=0 else returns -(timedelta) |
str(timedelta) | Returns a string in the form (+/-) day[s], HH:MM:SS.UUUUUU |
repr(timedelta) | Returns the string representation in the form of the constructor call |
示例 1:对 timedelta 对象执行基本算术运算。
蟒蛇3
from datetime import timedelta
# creating the timedelta object
t1 = timedelta(days=1)
print("Original timedelta:", t1)
# multiplication
t2 = t1*5.5
print("After Multiplication:", t2)
# Subtraction
res = t2 - t1
print("After Subtraction:", res)
# addition
res += t2
print("After Addition:", res)
# division
res = t2/2.5
print("After division:", res)
# floor division
res = t2 //2
print("After floor division:", res)
# Modulo
res = t2%timedelta(days=3)
print("After Modulo:", res)
Original timedelta: 1 day, 0:00:00
After Multiplication: 5 days, 12:00:00
After Subtraction: 4 days, 12:00:00
After Addition: 10 days, 0:00:00
After division: 2 days, 4:48:00
After floor division: 2 days, 18:00:00
After Modulo: 2 days, 12:00:00
示例 2:获取绝对值和 timedelta 对象的字符串表示
蟒蛇3
from datetime import timedelta
# creating the timedelta object
t1 = timedelta(days=1)
print("Original timedelta:", t1)
# Negatiob of timedelta object
t1 = -(t1)
print("After Negation:", t1)
# Getting Absolute value
t1 = abs(t1)
print("Absolute Value:", t1)
# Getting string representation
print("String representation:", str(t1))
# Getting Constructor call
print("Constructor call:", repr(t1))
Original timedelta: 1 day, 0:00:00
After Negation: -1 day, 0:00:00
Absolute Value: 1 day, 0:00:00
String representation: 1 day, 0:00:00
Constructor call: datetime.timedelta(1)
注意:有关Python日期时间的更多信息,请参阅Python日期时间教程