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

前端 未结 5 1152
独厮守ぢ
独厮守ぢ 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:25

    Example of tj111 suggestion:

    $("#ConditionValue").val(
        (
            new Date(
                "01/01/2000 " + 
                $("#EventCloseTimeHour option:selected").text() +
                $("#EventCloseTimeMin option:selected").text() + ":00" +
                " " +
                $("#EventCloseTimeMeridian option:selected").text()
            )
        ).toTimeString().slice(0,8))
    ;
    

    Or you can use:

    hour = hour %12 + (meridian === "AM"? 0 : 12);
    hour = hour < 10 ? "0" + hour : hour;  
    

提交回复
热议问题