I have a the following fields on the UI: datepicker, hours dropdown, minutes dropdown, and am/pm dropdown.
In my controller I am trying to tie these fields together and create a DateTime value like this (5/18/2012 2:45 PM):
model.Scheduled = new DateTime(model.Scheduled.Value.Year, model.Scheduled.Value.Month, model.Scheduled.Value.Day, model.ScheduledHour + (model.ScheduledAMPM == "AM" ? 0 : 12), model.ScheduledMinute, 0);
This works in most of the cases, but not when I select 12 on the hour dropdown. How should I refactor the above line of code so that it works in all cases?
Convert 12 AM and 12 PM to 0 and 12 by taking the Hour modulo 12:
I would also recommend writing unit tests to verify all edge cases.