What's a simple way to convert between an am/pm time to 24 hour time in javascript

前端 未结 5 1233
独厮守ぢ
独厮守ぢ 2021-02-03 15:51

I\'ve been playing with jquery and I\'ve run myself into a time problem. I\'ve got 3 time input select boxes, one for hours, one for minutes, and one for the Meridian. I need to

5条回答
  •  遇见更好的自我
    2021-02-03 16:26

    var time = "";
    var hour = Number($("#EventCloseTimeHour option:selected").text())%12 + ($("#EventCloseTimeMeridian option:selected").text() == 'PM'?12:0)) ;
    
    
    time +=("00"+String(hour)).slice(-2)+":"+ $("#EventCloseTimeMin option:selected").text();
    

    The key to this solution is using the conditional operator "a?b:c" which translates to "if a then b else c"

提交回复
热议问题