How can I add N milliseconds to a datetime in Python

后端 未结 2 622
感动是毒
感动是毒 2021-02-11 21:12

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 a

相关标签:
2条回答
  • 2021-02-11 21:51

    use timedelta:

    timedelta(microseconds=1000) #1 milli second
    
    0 讨论(0)
  • 2021-02-11 21:54

    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

    0 讨论(0)
提交回复
热议问题