python - Converting user input to time and substracting it from main time
问题描述
I am doing some project for myself and i am stuck with an assignment.
I have strict time that's 30min
I have a user that gives an input like: 24008
I need to convert an user input to time(2min:40sec:08milisec) and substract it from main time. I tried time.strftime('%M:%S:%f', 1800) to show main Time (30min) like 30:00:00 but i don't seem to get the datetime or time imports and how do they work. Same with user input.
Can anyone with a kind heart would guide me to a right path on how to get this logic done and by what function?
I can't share a code because i don't have any working logic for this one.
解决方案
datetime.time对象可能是为此使用的最佳数据结构
您的初始值 30 分钟将定义如下
import datetime
strict_time = datetime.time(minutes=30)
如果您作为示例给出的用户输入始终是输入格式,那么解析起来会有点困难,因为 python 的 datetime 模块的 strptime 行为不能处理 2 位数毫秒输入和 1 位数分钟输入。如果输入格式完全相同(5 位数字,1 分钟数字,2 秒数字和 2 毫秒数字),那么以下将起作用
user_input = '24008'
input_time = datetime.timedelta(
minutes=int(user_input[0]),
seconds=int(user_input[1:2]),
microseconds=int(user_input[3:4])
)
new_time = strict_time - input_time
推荐阅读
- powershell - PowerShell“网络使用”错误
- javascript - 无法重置 cookie
- python - python中的峰值查找算法错误
- react-native - 反应导航在其他 StackNavigator 中制作透明屏幕
- vba - 尝试更改单元格中特定字符的大小写
- sql - 获取根元素和所有后代 MS SQL
- javascript - 获取 d.getTime 不是函数
- php - 使用相关字段自动填充多个文件 - SuiteCRM 版本 7.10.4 Sugar 版本 6.5.25 (Build 344)
- gulp - Gulp 目录更改
- python-3.x - GET在python中返回400