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

后端 未结 27 3114
慢半拍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:05

    Please find the solution below

    var d = new Date();
    var amOrPm = (d.getHours() < 12) ? "AM" : "PM";
    var hour = (d.getHours() < 12) ? d.getHours() : d.getHours() - 12;
    return   d.getDate() + ' / ' + d.getMonth() + ' / ' + d.getFullYear() + ' ' + hour + ':' + d.getMinutes() + ' ' + amOrPm;
    

提交回复
热议问题