python - convert a time string to time
问题描述
I have two time strings for start and end, as an example:
start = "9:30:00"
end = "11:14:00"
I want to subtract start from end to get the duration.
I have tried
new_start = datetime.strptime(start, '%H:%M:%S').strftime("%H:%M:%S")
new_end = datetime.strptime(end, '%H:%M:%S').strftime("%H:%M:%S")
I still get
unsupported operand type(s) for -: 'str' and 'str'
解决方案
To convert a time string to a time, use strptime
.
...and that's it.
Don't convert the times to strings again with strftime
before you have done your calculations with them.
推荐阅读
- python - Python 3:列表索引问题
- flutter - 在 Flutter 中将 SystemSounds 添加到 PageView
- css - 部分不会出现在移动设备上,但会出现在开发工具中
- ios - UIPicker 视图未填充
- python - Homebrew pyenv安装错误dyld:未加载库:/usr/local/opt/readline/lib/libreadline.7.dylib
- arduino - 在 Arduino 中校准 Adafruit LSM303
- html - 如何在 td 中显示背景图像,而不是文本?
- java - 将本地 jar(oracle 驱动程序)添加到 Maven 和 Bamboo
- c# - 将 CHAR_INFO.Attributes 转换为 ConsoleColor
- c++ - 在 QChart 上画一个点