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

后端 未结 27 3099
慢半拍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 02:57

    you can determine am or pm with this simple code

    var today=new Date();
    var noon=new Date(today.getFullYear(),today.getMonth(),today.getDate(),12,0,0);
    var ampm = (today.getTime()<noon.getTime())?'am':'pm';
    
    0 讨论(0)
  • 2020-11-22 02:59

    var d = new Date();
    var hours = d.getHours() % 12;
      hours = hours ? hours : 12;
        var test = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][(d.getMonth() + 1)] + " " + 
        ("00" + d.getDate()).slice(-2) + " " + 
        d.getFullYear() + " " + 
        ("00" + hours).slice(-2) + ":" + 
        ("00" + d.getMinutes()).slice(-2) + ":" + 
        ("00" + d.getSeconds()).slice(-2) + ' ' + (d.getHours() >= 12 ? 'PM' : 'AM'); 
        
    document.getElementById("demo").innerHTML = test;
    <p id="demo" ></p>

    0 讨论(0)
  • 2020-11-22 03:01

    Or just simply do the following code:

        <script>
            time = function() {
                var today = new Date();
                var h = today.getHours();
                var m = today.getMinutes();
                var s = today.getSeconds();
                m = checkTime(m);
                s = checkTime(s);
                document.getElementById('txt_clock').innerHTML = h + ":" + m + ":" + s;     
                var t = setTimeout(function(){time()}, 0);
            }
    
            time2 = function() {
                var today = new Date();
                var h = today.getHours();
                var m = today.getMinutes();
                var s = today.getSeconds();
                m = checkTime(m);
                s = checkTime(s);
                if (h>12) {
                    document.getElementById('txt_clock_stan').innerHTML = h-12 + ":" + m + ":" + s;
                }               
                var t = setTimeout(function(){time2()}, 0);
            }
    
            time3 = function() {
                var today = new Date();
                var h = today.getHours();
                var m = today.getMinutes();
                var s = today.getSeconds();
                if (h>12) {
                    document.getElementById('hour_line').style.width = h-12 + 'em'; 
                }
                document.getElementById('minute_line').style.width = m + 'em';  
                document.getElementById('second_line').style.width = s + 'em';  
                var t = setTimeout(function(){time3()}, 0);
            }
    
            checkTime = function(i) {
                if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
                return i;
            }           
        </script>
    

    0 讨论(0)
  • 2020-11-22 03:02

    In modern browsers, use Intl.DateTimeFormat and force 12hr format with options:

        let now = new Date();
    
        new Intl.DateTimeFormat('default',
            {
                hour12: true,
                hour: 'numeric',
                minute: 'numeric'
            }).format(now);
    
        // 6:30 AM
    

    Using default will honor browser's default locale if you add more options, yet will still output 12hr format.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题