我有两个没有AM / PM的时间戳属性。所需要的是找到由_factor确定的季度单位花费的总时间。然后有一个向下&由_roundDown值确定为截止点的综合概念。最后,在计算后返回一个十进制值。
这存在于设置属性TotalTime的类定义中。由于这个原因,我希望代码有效。请原谅我的代码,因为这是项目的最后一部分,并希望完成它。
如何改进代码:我正在使用mod计算。
private decimal ComputeTotalTime(int _increment)
{
decimal _TotalHours = 0;
int _roundDown = 7, _factor = 15;
int int_TotalHours = 0;
if (String.IsNullOrEmpty(StartTime) || String.IsNullOrEmpty(EndTime)) return _TotalHours;
DateTime timeFromInput = DateTime.ParseExact(StartTime, "H:m", null);
DateTime timeToInput = DateTime.ParseExact(EndTime, "H:m", null);
//Here found a problem that we need to ensure that EndTime is greater than StartTime
//In one run, StartTime is 10:30 and EndTime is 2:32
//but the DateTime variables took is as 10:30 PM and 2:32 PM hence producing negative difference
TimeSpan ts = timeToInput.Subtract(timeFromInput)
int_TotalHours = ts.Hours * 60 + ts.Minutes;
if (int_TotalHours % _factor == 0) { /*I'm Perfect, no need to round*/ }
else if (int_TotalHours % _factor <= _roundDown) {
//Round down to nearest 15th, quarter
int_TotalHours = int_TotalHours - (int_TotalHours % _factor); }
else { //Round up to nearest quarter
int_TotalHours = int_TotalHours + (_factor - int_TotalHours % _factor); }
_TotalHours = Convert.ToDecimal(int_TotalHours / 60.00);
_TotalHours = (_TotalHours * 100) / 100;
return _TotalHours;
}
感谢您的帮助。