Last updated: Apr 8, 2024
Reading timeยท3 min
Use the timedelta()
class from the datetime
module to add hours to
datetime, e.g. result = dt + timedelta(hours=10)
.
The timedelta
class can be passed a hours
argument and adds the specified
number of hours to the datetime.
from datetime import datetime, timedelta d = '2023-11-24 09:30:00.000123' # ๐๏ธ convert string to datetime object dt = datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f') print(dt) # ๐๏ธ 2023-11-24 09:30:00.000123 result = dt + timedelta(hours=3) print(result) # ๐๏ธ 2023-11-24 12:30:00.000123
09:30:15
scroll down to the last code snippet.Make sure to import the datetime
and
timedelta
classes from the datetime
module.
The example creates a datetime
object from a datetime
string and adds hours
to it.
from datetime import datetime, timedelta d = '2023-11-24 09:30:00.000123' # ๐๏ธ convert string to datetime object dt = datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f') print(dt) # ๐๏ธ 2023-11-24 09:30:00.000123 result_1 = dt + timedelta(hours=3) print(result_1) # ๐๏ธ 2023-11-24 12:30:00.000123
The datetime.strptime() method returns a datetime object that corresponds to the provided date string, parsed according to the format.
If you have a date string that is formatted in a different way, use
this table of the docs
to look up the format codes you should pass as the second argument to the
strptime()
method.
datetime
class to create a datetime
objectThe following example uses the
datetime
class to create a datetime
object and the timedelta
class to add hours to
it.
from datetime import datetime, timedelta dt = datetime(2023, 9, 24, 9, 30, 35) print(dt) # ๐๏ธ 2023-09-24 09:30:35 result = dt + timedelta(hours=4) print(result) # ๐๏ธ 2023-09-24 13:30:35
We passed values for the year
, month
, day
, hour
, minute
and seconds
arguments.
If you need to add hours to the current time, use the datetime.today()
method
to get a datetime
object that stores the current time.
from datetime import datetime, timedelta # โ Add hours to current time now = datetime.today() print(now) # ๐๏ธ 2023-02-19 05:40:02.239652 result = now + timedelta(hours=5) print(result) # ๐๏ธ 2023-02-19 10:40:02.239652
The datetime.today() method returns the current local datetime.
datetime
object because it automatically rolls over the days, months and years if necessary.This wouldn't be possible if we only had the time component. For example,
11:59:30PM
+ 2
hours would raise an exception.
If you only need to extract the time after the operation, call the time()
method on the datetime
object.
from datetime import datetime, timedelta now = datetime.now() print(now) # ๐๏ธ 2023-02-19 05:41:07.814924 result = now + timedelta(hours=5) print(result) # ๐๏ธ 2023-02-19 10:41:07.814924 # โ Only get updated time print(result.time()) # ๐๏ธ 10:41:07.814924
The datetime.time() method returns a time object with the same hour, minute, second and millisecond.
If you need to get the time formatted as HH:MM:SS
, use a formatted string
literal.
from datetime import datetime, timedelta now = datetime.now() print(now) # ๐๏ธ 2023-02-19 05:41:40.114944 result = now + timedelta(hours=3) print(result) # ๐๏ธ 2023-02-19 08:41:40.114944 print(result.time()) # ๐๏ธ 08:41:40.114944 # ๐๏ธ format as HH:MM:SS print(f'{result:%H:%M:%S}') # ๐๏ธ 08:41:40
Formatted string literals
(f-strings) let us include expressions inside of a string by prefixing the
string with f
.
Make sure to wrap expressions in curly braces - {expression}
.
Formatted string literals also enable us to use the format specification mini-language in expression blocks.
If you only have the time component, use the datetime.combine
method to
combine the time with the current (or some other) date and get a datetime
object.
from datetime import datetime, date, timedelta, time t = time(6, 25) print(t) # ๐๏ธ 06:25:00 result = datetime.combine(date.today(), t) + timedelta(hours=6) print(result) # ๐๏ธ 2023-02-19 12:25:00 only_t = result.time() print(only_t) # ๐๏ธ 12:25:00
The
datetime.combine()
method takes a date
and time
as arguments and returns a new datetime
object by combining them.
Once we get a datetime
object, we can use the timedelta
class to add hours
to it.
You can learn more about the related topics by checking out the following tutorials: