How do you display JavaScript datetime in 12 hour AM/PM format?

后端 未结 27 3412
慢半拍i
慢半拍i 2020-11-22 02:36

How do you display a JavaScript datetime object in the 12 hour format (AM/PM)?

27条回答
  •  甜味超标
    2020-11-22 03:15

    If you don't need to print the am/pm, I found the following nice and concise:

    var now = new Date();
    var hours = now.getHours() % 12 || 12;  // 12h instead of 24h, with 12 instead of 0. 
    

    This is based off @bbrame's answer.

提交回复
热议问题