Last updated: Apr 8, 2024
Reading timeยท3 min

Use the timedelta() class from the datetime module to add milliseconds to
datetime.
The timedelta class can be passed a milliseconds argument and adds the
specified number of milliseconds to the datetime object.
from datetime import datetime, timedelta d = '2023-11-24 09:30:00.000123' # ๐๏ธ Convert a string to a 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(milliseconds=300) print(result) # ๐๏ธ 2023-11-24 09:30:00.300123

09:30:13.000123 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
milliseconds to it.
from datetime import datetime, timedelta d = '2023-11-24 09:30:00.000123' # ๐๏ธ Convert a string to a 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(milliseconds=300) print(result) # ๐๏ธ 2023-11-24 09:30:00.300123
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
milliseconds 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(milliseconds=400) print(result) # ๐๏ธ 2023-09-24 09:30:35.400000

We passed values for the year, month, day, hour, minute and second
arguments.
If you need to add milliseconds to the current time, use the datetime.today()
method to get a datetime object that stores the current time.
from datetime import datetime, timedelta now = datetime.today() print(now) # ๐๏ธ 2023-07-22 10:38:19.223431 result = now + timedelta(milliseconds=500) print(result) # ๐๏ธ 2023-07-22 10:38:19.723431

The
datetime.today()
method returns the current local datetime.
datetime object because it automatically rolls over the seconds, minutes, hours, days, months and years if necessary.This wouldn't be possible if we only had the time component. For example,
11:59:59.778231 PM + 5000 milliseconds would raise an exception.
datetime.combine method to add milliseconds to a timeIf 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, 30, 123) print(t) # ๐๏ธ 06:25:30.000123 result = datetime.combine(date.today(), t) + timedelta(milliseconds=400) print(result) # ๐๏ธ 2023-07-22 06:25:30.400123 only_t = result.time() print(only_t) # ๐๏ธ 06:25:30.400123
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
milliseconds to it.
datetimeUse 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(6, 25, 30, 123) print(t) # ๐๏ธ 06:25:30.000123 result = datetime.combine(date.today(), t) + timedelta(milliseconds=400) print(result) # ๐๏ธ 2023-07-22 06:25:30.400123 # โ Only get the updated time only_t = result.time() print(only_t) # ๐๏ธ 06:25:30.400123

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: