Issue: Convert 12hr clock to 24 hrs, then reverse it back

后端 未结 2 1678
遇见更好的自我
遇见更好的自我 2021-01-27 04:05

I am a c# beginner. I am writing a web application in MVP, and having trouble to convert 12 hr clock to 24 hr clock. So, there are three dropdown boxes (hour, mins, AM/PM) When

2条回答
  •  孤街浪徒
    2021-01-27 04:45

    There is some simple math you can do to convert between hours of a 24-hour clock and hours of a 12-hour clock.

    // 24-hour clock to 12-hour clock
    int hours12 = (hours24 + 11) % 12 + 1;
    string meridiem = hours24 < 12 ? "AM" : "PM";
    
    // 12-hour clock to 24-hour clock
    int hours24 = meridiem == "AM"
      ? (hours12 == 12 ? 0 : hours12)
      : (hours12 == 12 ? 12 : hours12 + 12);
    

提交回复
热议问题