How can I add N milliseconds to a datetime in Python

Question:

I’m setting a datetime var as such:

fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")

where date and time are string of the appropriate nature for datetime. How can I increment this datetime by N milliseconds?

Asked By: WildBill

||

Answers:

Use timedelta

To increment by 500 ms:

fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)

You can use it to increment minutes, hours, days etc. Documentation:

https://docs.python.org/2/library/datetime.html#timedelta-objects

Answered By: Martin Konecny

use timedelta:

timedelta(microseconds=1000) #1 milli second
Answered By: venpa
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.