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

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

    Here my solution

    function getTime() {
    var systemDate = new Date();
    var hours = systemDate.getHours();
    var minutes = systemDate.getMinutes();
    var strampm;
    if (hours >= 12) {
        strampm= "PM";
    } else {
        strampm= "AM";
    }
    hours = hours % 12;
    if (hours == 0) {
        hours = 12;
    }
    _hours = checkTimeAddZero(hours);
    _minutes = checkTimeAddZero(minutes);
    console.log(_hours + ":" + _minutes + " " + strampm);
    }
    
    function checkTimeAddZero(i) {
    if (i < 10) {
        i = "0" + i
    }
    return i;
    }
    

提交回复
热议问题