如何在 Python 中将 N 毫秒添加到日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24522793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:44:44  来源:igfitidea点击:

How can I add N milliseconds to a datetime in Python

pythondatetime

提问by WildBill

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?

其中日期和时间是适合日期时间的字符串。如何将此日期时间增加 N 毫秒?

采纳答案by Martin Konecny

Use timedelta

timedelta

To increment by 500 ms:

增加 500 毫秒:

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

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

回答by venpa

use timedelta:

使用时间增量

timedelta(microseconds=1000) #1 milli second