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?
strptime() function in python converts the string into DateTime objects. The strptime() is a class method that takes two arguments : string that should be converted to datetime object.
Use the timedelta() class from the datetime module to add seconds to datetime, e.g. result = dt + timedelta(seconds=24) . The timedelta class can be passed a seconds argument and adds the specified number of seconds to the datetime.
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.
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
use timedelta:
timedelta(microseconds=1000) #1 milli second
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With