Python - 带有负值的 Timedelta 对象
在Python,有 Datetime 库,它是timedelta()函数所在的内置库。使用此函数,我们可以找出未来的日期和时间或过去的日期和时间。在 timedelta() 对象中,delta 表示两个日期或时间之间的差异。
句法:
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minute=0, hours=0, weeks=0)
所有参数都是可选的,默认为 0。我们可以在参数中传递正值和负值。
让我们看一些示例以更好地理解该主题。
示例 1:使用具有负值的 timedelta 对象查找估计日期。
Python3
# importing datetime and timedelta from
# datetime module
from datetime import datetime, timedelta
# function to return date
def get_date(current_date):
# creating timedelta object with negative
# values
date_obj = timedelta(days=-534)
# getting required date
req_date = current_date + date_obj
# splitting date from resultant datetime
date = req_date.date()
# returning date
return date
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the date
resulted_date = get_date(current_datetime)
# printing current date
print('Current date is:', current_datetime.date())
# printing resultant date after using timedelta
print('Resultant time after using timedelta object is:',
resulted_date)
Python3
# importing datetime and timedelta from
# datetime module
from datetime import datetime, timedelta
# function to return time
def get_time(current_time):
# creating timedelta object with negative
# values
time_obj = timedelta(hours=-12, minutes=-15)
# getting required time
req_time = current_time + time_obj
# splitting time from resultant datetime
time = req_time.time()
# returning time
return time
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the time
resulted_time = get_time(current_datetime)
# printing current time
print('Current time is:', current_datetime.time())
# printing resultant time after using timedelta
print('Resultant time after using timedelta object is:',
resulted_time)
Python3
# importing datetime and timedelta from
# datetime module
from datetime import datetime, timedelta
# function to return time
def get_time(current_time):
# creating timedelta object with negative
# values
time_obj = timedelta(hours=15, minutes=25)
# getting required time
req_time = current_time - time_obj
# splitting time from resultant datetime
time = req_time.time()
# returning time
return time
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the time
resulted_time = get_time(current_datetime)
# printing current time
print(f'Current time is: {current_datetime.time()}')
# printing resultant time after using timedelta
print(f'Resultant time after using timedelta object is: {resulted_time}')
Python3
# importing datetime and timedelta from datetime module
from datetime import datetime,timedelta
# function to return time
def get_datetime(current_datetime):
# creating timedelta object with negative values
time_obj = timedelta(weeks=-1, days=-4,
hours=-15, minutes=-25,
seconds=-54)
# getting required time and time
req_time = current_datetime + time_obj
# returning date and time
return req_time
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the date and time
resulted_time = get_datetime(current_datetime)
# printing current date and time
print(f'Current time is: {current_datetime}')
# printing resultant date and time after using timedelta
print(f'Resultant time after using timedelta object is: {resulted_time}')
输出:
Current date is: 2021-03-24
Resultant time after using timedelta object is: 2019-10-07
上面的例子显示了当前日期和使用 timedelta 对象后的日期,在上面的例子中,我们在 timedelta 对象中传递了 days=-534 作为参数。负值代表过去,正值代表未来,在上面的代码中,我们已经过去了 -534 天,这意味着我们得到的日期是从今天的日期回溯 534 天。
示例 2:使用具有负值的 timedelta 对象查找估计时间。
蟒蛇3
# importing datetime and timedelta from
# datetime module
from datetime import datetime, timedelta
# function to return time
def get_time(current_time):
# creating timedelta object with negative
# values
time_obj = timedelta(hours=-12, minutes=-15)
# getting required time
req_time = current_time + time_obj
# splitting time from resultant datetime
time = req_time.time()
# returning time
return time
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the time
resulted_time = get_time(current_datetime)
# printing current time
print('Current time is:', current_datetime.time())
# printing resultant time after using timedelta
print('Resultant time after using timedelta object is:',
resulted_time)
输出:
Current time is: 15:53:37.019928
Resultant time after using timedelta object is: 03:38:37.019928
上面的例子显示了当前时间和使用 timedelta 对象后的时间,在上面的代码中,我们通过了 hours=-12 和 minutes=-15 这意味着我们得到了结果时间,即从现在开始的 12 小时 15 分钟之前。
示例 3:通过使用具有负值的 timedelta 对象来查找估计时间的另一种方法。
蟒蛇3
# importing datetime and timedelta from
# datetime module
from datetime import datetime, timedelta
# function to return time
def get_time(current_time):
# creating timedelta object with negative
# values
time_obj = timedelta(hours=15, minutes=25)
# getting required time
req_time = current_time - time_obj
# splitting time from resultant datetime
time = req_time.time()
# returning time
return time
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the time
resulted_time = get_time(current_datetime)
# printing current time
print(f'Current time is: {current_datetime.time()}')
# printing resultant time after using timedelta
print(f'Resultant time after using timedelta object is: {resulted_time}')
输出:
Current time is: 15:52:59.538796
Resultant time after using timedelta object is: 00:27:59.538796
在上面的例子中,我们在 timedelta 对象中传递了正值,但它们表现得像负值,因为在上面代码的第 9 行找到 required_time 时,我们在 timedelta 对象中使用了负号,因此参数的值通过正面自动变为负面,我们得到结果时间,即从现在开始的 15 小时 25 分钟之前。
示例 4:使用具有负值的 timedelta 对象查找估计日期和时间。
蟒蛇3
# importing datetime and timedelta from datetime module
from datetime import datetime,timedelta
# function to return time
def get_datetime(current_datetime):
# creating timedelta object with negative values
time_obj = timedelta(weeks=-1, days=-4,
hours=-15, minutes=-25,
seconds=-54)
# getting required time and time
req_time = current_datetime + time_obj
# returning date and time
return req_time
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the date and time
resulted_time = get_datetime(current_datetime)
# printing current date and time
print(f'Current time is: {current_datetime}')
# printing resultant date and time after using timedelta
print(f'Resultant time after using timedelta object is: {resulted_time}')
输出:
Current time is: 2021-03-24 15:51:33.024268
Resultant time after using timedelta object is: 2021-03-13 00:25:39.024268
上面的例子显示了使用timedelta对象后的当前日期和时间以及结果日期和时间,在上面的代码中我们传递了weeks=-1 days=-4, hours=-15, minutes=-25, seconds=-54 timedelta 对象意味着我们在使用 timedelta 对象后获得估计的日期和时间是从现在开始的 1 周 4 天 15 小时 25 分 54 秒。