
Python timedelta - How to add minutes or hours to the current time
We can get the future date or time to use Python timedelta
method. Before using that, let's import datetime
and timedelta
from datetime
.
from datetime import datetime, timedelta
now = datetime.now()
future = now + timedelta(minutes=6)
print(now) # 2020-09-26 14:47:31.683936
print(future) # 2020-09-26 14:53:31.683936
datetime.now
returns now date (or time). If you want to add 6 minutes, add now timedelta(minutes=6)
.
now and future is datetime.datetime
object and has year, month, day, hour, minute and second attributes.
from datetime import datetime, timedelta
now = datetime.now()
future = now + timedelta(minutes=6)
print(now) # 2020-09-26 14:52:24.807849
print(future) # 2020-09-26 14:58:24.807849
print(type(now)) # <class 'datetime.datetime'>
print(type(future)) # <class 'datetime.datetime'>
print(future.year) # 2020
print(future.month) # 9
print(future.day) # 26
print(future.hour) # 14
print(future.minute) # 58
print(future.second) # 24
print(future.microsecond) # 807849
print(future.weekday()) # 5
print(future.isoweekday()) # 6
Comments
Powered by Markdown