Last updated: Apr 8, 2024
Reading timeยท4 min
Use the timedelta()
class from the datetime
module to add minutes to
datetime, e.g. result = dt + timedelta(minutes=10)
.
The timedelta
class can be passed a minutes
argument and adds the
specified number of minutes to the datetime
object.
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(minutes=29) print(result) # ๐๏ธ 2023-11-24 09:59:00.000123
09:30:13
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
minutes 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 = dt + timedelta(minutes=29) print(result) # ๐๏ธ 2023-11-24 09:59: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
object using the timedelta
classThe following example uses the
datetime
class to create a datetime
object and the timedelta
class to add minutes 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(minutes=15) print(result) # ๐๏ธ 2023-09-24 09:45:35
We passed values for the year
, month
, day
, hour
, minute
and second
arguments.
You can use the datetime.today()
method to get a datetime
object that
represents the current date and time to add minutes to the current time.
from datetime import datetime, timedelta now = datetime.today() print(now) # ๐๏ธ 2023-02-19 05:32:39.521250 result = now + timedelta(minutes=5) print(result) # ๐๏ธ 2023-02-19 05:37:39.521250
The
datetime.today()
method returns the current local datetime
.
The timedelta
class can be passed the days
, weeks
, hours
, minutes
,
seconds
, milliseconds
and microseconds
as arguments.
All of the arguments are optional and default to 0
.
It's best to only use keyword arguments in a call to the timedelta
class as
the order of arguments can be confusing.
We only provided a value for the minutes
argument in the example.
datetime
object because it automatically rolls over the hours, days, months and years if necessary.This wouldn't be possible if we only had the time component. For example,
11:59:30PM
+ 15
minutes would raise an exception.
time()
method to extract the time after the operationIf 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:33:06.313209 result = now + timedelta(minutes=25) print(result) # ๐๏ธ 2023-02-19 05:58:06.313209 # โ only get updated time print(result.time()) # ๐๏ธ 05:58:06.313209
We used the datetime.now method to get the current local date and time.
now
variable stores a datetime
object, to which we can add minutes using the timedelta
class.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:33:54.666803 result = now + timedelta(minutes=25) print(result) # ๐๏ธ 2023-02-19 05:58:54.666803 print(result.time()) # ๐๏ธ 05:58:54.666803 # ๐๏ธ format as HH:MM:SS print(f'{result:%H:%M:%S}') # ๐๏ธ 05:58:54
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.
datetime.combine
if you only have the time componentIf 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(9, 25) print(t) # ๐๏ธ 09:25:00 result = datetime.combine(date.today(), t) + timedelta(minutes=30) print(result) # ๐๏ธ 2023-02-19 09:55:00 only_t = result.time() print(only_t) # ๐๏ธ 09:55: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 minutes
to it.
Use the time()
method on the datetime
object if you only need to extract the
time after the operation.
from datetime import datetime, date, timedelta, time t = time(9, 25) print(t) # ๐๏ธ 09:25:00 result = datetime.combine(date.today(), t) + timedelta(minutes=30) print(result) # ๐๏ธ 2023-02-19 09:55:00 # โ Only get updated time only_t = result.time() print(only_t) # ๐๏ธ 09:55:00
The datetime.time() method returns a time object with the same hour, minute, second and millisecond.
You can learn more about the related topics by checking out the following tutorials: