如何以12小时格式(AM/PM)显示JavaScript datetime对象?


当前回答

下面是一种使用regex的方法:

console.log(新日期(“7/10/2013 20:12:34”).toLocaleTimeString () .replace (/ ((\ d) +: \ d {2}) (: \ [d] {2 })(.*)/, "$ 1美元3”)) console.log(新日期(“7/10/2013 01:12:34”).toLocaleTimeString () .replace (/ ((\ d) +: \ d {2}) (: \ [d] {2 })(.*)/, "$ 1美元3”))

这将创建3个匹配组:

([\d]+:[\d]{2}) -小时:分钟 (:[\d]{2}) -秒 (.*) -空格和时段(时段是AM/PM的正式名称)

然后显示第1组和第3组。

警告:toLocaleTimeString()可能会根据区域/位置表现不同。

其他回答

它将返回如下格式

09:56 AM

如果小时数小于10,则在start中添加0

这里使用的是ES6语法

const getTimeAMPMFormat = (date) => { let hours = date. gehours (); let minutes = date.getMinutes(); Const ampm =小时>= 12 ?' pm ': ' am '; 小时=小时% 12; 小时=小时?时长:12小时;//小时'0'应该是'12' 小时=小时< 10 ?'0' + hours:小时; //如果hours小于10,则在开始时添加0 分钟=分钟< 10 ?'0' + minutes: minutes; 返回小时+ ':' +分钟+ ' ' + ampm; }; console.log (getTimeAMPMFormat(新日期));// 09:59 am

函数formatAMPM(date) { var hours = date.getHours(); var minutes = date.getMinutes(); Var ampm =小时>= 12 ?'pm': 'am'; 小时=小时% 12; 小时=小时?时长:12小时;//小时'0'应该是'12' 分钟=分钟< 10 ?'0'+minutes: minutes; var strTime = hours + ':' + minutes + ' + ampm; 返回strTime; } console.log (formatAMPM(新日期));

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>

试试这个

      var date = new Date();
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var seconds = date.getSeconds();
      var ampm = hours >= 12 ? "pm" : "am";

或者只是简单地执行以下代码:

    <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>