首页 > 解决方案 > 如何用军事时间进行简单的计算?

问题描述

我正在处理 asp.net mvc 应用程序中的一个问题,我需要将军事时间作为字符串,例如“2200”,然后我将持续时间作为双倍,可能类似于 3.5站了3个半小时。

然后我需要确定开始时间和持续时间是否大于或等于“2400”,这意味着持续时间进入第二天。

我不知道该怎么做,如果我需要包含一个标题,以便在军事时间进行简单的添加。我知道我可以将字符串转换为 int,但是整数是十进制的,而时间不是,所以我不知道它是如何工作的。

感谢您提供任何帮助,感谢您花时间准备我的问题!

标签: c#asp.net-mvc

解决方案


//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);

推荐阅读