How to convert time from 24 hour format to 12 hour format using javascript?

后端 未结 5 1715
逝去的感伤
逝去的感伤 2021-01-03 00:55

The function returns the time in 24 hour format.

function fomartTimeShow(h) {
    return h < 10 ? \"0\" + h + \":00\" : h + \":00\";
}
5条回答
  •  感情败类
    2021-01-03 01:27

    You could try something like the following:

    function fomartTimeShow(h) { 
        var ampm = "am"
        if (h >= 12)
            ampm = "pm"
        h = h % 12;
        if (h == 0)
         h = 12;
        return h ":00" + ampm; 
    }
    

提交回复
热议问题