python - 将时间添加到特定时间戳的 Python 代码是什么?

标签 python datetimeoffset

试图找出 datetime 模块并需要一些帮助。

我有一个看起来像这样的字符串:

00:03:12,200 --> 00:03:14,316

(即时:分:秒,毫秒) 并且需要为每个时间戳添加 10 秒。作为输出:

00:03:22,200 --> 00:03:24,316

执行此操作的 Python 代码是什么?

最佳答案

要解析时间,使用strptime

>>> x = '00:03:12,200 --> 00:03:14,316'
>>> x1 = x.split('-->')
>>> x1
['00:03:12,200 ', ' 00:03:14,316']
>>> t1 = datetime.datetime.strptime(x1[0], '%H:%M:%S,%f ')
>>> t1
datetime.datetime(1900, 1, 1, 0, 3, 12, 200000)

使用时间增量

>>> t = datetime.timedelta(seconds=1)
>>> t
datetime.timedelta(0, 1)
>>> 
>>> t1 + t
datetime.datetime(1900, 1, 1, 0, 3, 13, 200000)
>>> k = t1 + t

重新转换为字符串

>>> k.strftime('%H:%M:%S,%f ')
'00:03:13,200000 '

关于python - 将时间添加到特定时间戳的 Python 代码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4363072/

相关文章:

python - 根据值附加到存储在数据帧中的字典

python - PyPI:上传新版本时模块代码不会更新

java - 如果字符串包含 000,OffsetDateTime 不会显示毫秒

python - 如何使用 matplotlib.pyplot 更改子图的宽度/高度比?

python - Tensorflow Precision、Recall、F1 - 多标签分类

datetime - 在 Go 中,给定一个位置名称,我们如何确定该位置的当前时间?

java - 在 Jersey 消费 OffsetDateTime

c# - 为什么不 DateTime.ToString ("R") 和 DateTime.TryParseExact 往返?

python - 模拟量子谐振子/SHM

c# - 来自字符串的新 DateTimeOffset