I need the current Military time (ex 906 equals 9:06) rounded to the nearest ten. So I need ten minute increments. Instead of 906 I need 910, and instead of 903 I need 900. I've tried the using the System.Math.Round function but it only takes decimals and it doesn't round whole numbers.
Any help would be greatly appreciated!!
Thanks,
Chris
Hi Chris,
Here's a start. This code rounds military time to the nearest ten. Modify the last line if you need the output in a different format.
string timeStr = "2259";
timeStr = timeStr.Insert(timeStr.Length-2,":");
DateTime nowTime = Convert.ToDateTime(timeStr);
decimal dTimeMin = Convert.ToDecimal(nowTime.Minute);
decimal dMinRound = dTimeMin / 10;
dMinRound = System.Math.Round(dMinRound);
dMinRound = dMinRound * 10;
double diffVal = (int)dMinRound - (int)dTimeMin;
DateTime myResult = nowTime.AddMinutes(diffVal);
Response.Write(myResult.TimeOfDay.ToString());
Hope this helps.
-- Erik Reitan
ASP.NET User Education
This posting is provided "AS IS" with no warranties, and confers no rights.
0 comments:
Post a Comment