c# - 如何用军事时间进行简单的计算?
问题描述
我正在处理 asp.net mvc 应用程序中的一个问题,我需要将军事时间作为字符串,例如“2200”,然后我将持续时间作为双倍,可能类似于 3.5站了3个半小时。
然后我需要确定开始时间和持续时间是否大于或等于“2400”,这意味着持续时间进入第二天。
我不知道该怎么做,如果我需要包含一个标题,以便在军事时间进行简单的添加。我知道我可以将字符串转换为 int,但是整数是十进制的,而时间不是,所以我不知道它是如何工作的。
感谢您提供任何帮助,感谢您花时间准备我的问题!
解决方案
//define military time format string
string _format = "HHmm";
//your time
string time = "2200";
//parse string to DateTime using defined format
DateTime normalTime = DateTime.ParseExact(time, _format, System.Globalization.CultureInfo.InvariantCulture);
//add hours to DateTime variable
DateTime newTime = normalTime.AddHours(3.5);
//convert DateTime to military time string using defined format string
string newTimeText = newTime.ToString(_format);
推荐阅读
- spring - 如何在 Intellij 中正确创建资源文件夹?
- javascript - 文本上的 CSS 混合模式变为透明或无效
- javascript - 是否可以为快速会话设置 cookie 永不过期?
- firebase - Firebase 到 bigquery 的集成,我怎么知道数据准备好了?
- javascript - 无法读取未定义的属性(读取“值”)React 功能组件
- java - 为什么 AQS 的打印方式不同
- android - 使用 flick_video_player 显示视频后,状态栏变黑
- google-places-api - 迁移到 Google Places API:鸡和蛋的问题
- angular - Angular-如何使用反应形式构建具有键和值的动态对象
- python - 如何根据条件删除一系列行?