Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中以12小时格式显示时间_Javascript_Date_Time - Fatal编程技术网

在JavaScript中以12小时格式显示时间

在JavaScript中以12小时格式显示时间,javascript,date,time,Javascript,Date,Time,我想通过修改以下代码以12小时格式显示时间。 我尝试了各种技术,但没有运气,希望从你们那里找到解决办法 <script type="text/javascript"> $.fn.androClock = function() { var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; var months = ["Jan", "Feb", "Mar", "A

我想通过修改以下代码以12小时格式显示时间。 我尝试了各种技术,但没有运气,希望从你们那里找到解决办法

<script type="text/javascript">

$.fn.androClock = function() {
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"];
function getTime() {
  var date = new Date(),
  hour = date.getHours();
  return {
    day: days[date.getDay()],
    date: date.getDate(),
    month: months[date.getMonth()],
    hour: appendZero(hour),
    minute: appendZero(date.getMinutes())
  };
}
function appendZero(num) {
  if (num < 10) {
    return "0" + num;
  }
  return num;
}
function refreshClock() {
  var now = getTime();
  $('#date').html(now.day + "<br>" + now.date + '. ' + now.month);
  $('#time').html(now.hour + ":" + now.minute);
  setTimeout(function() {
    refreshClock();
  }, 10000);
}
refreshClock();
  };
$('#andro-clock').androClock();

</script>

$.fn.androClock=函数(){
风险值天数=[“周日”、“周一”、“周二”、“周三”、“周四”、“周五”、“周六”];
风险值月份=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
函数getTime(){
变量日期=新日期(),
hour=date.getHours();
返回{
天:天[date.getDay()],
日期:date.getDate(),
月份:月份[日期.getMonth()],
小时:0(小时),
分钟:appendZero(date.getMinutes())
};
}
函数0(num){
如果(数值<10){
返回“0”+num;
}
返回num;
}
函数刷新时钟(){
var now=getTime();
$(“#date').html(now.day+”
“+now.date+”.+now.month); $('#time').html(now.hour+“:“+now.minute); setTimeout(函数(){ 刷新时钟(); }, 10000); } 刷新时钟(); }; $(“#安卓时钟”).androClock();

函数getTime(){ 变量日期=新日期(), hour=date.getHours(); //var dd=“AM”; var h=小时; 如果(h>12){ h=12小时; //dd=“PM”; } 如果(h==0){ h=12; } 返回{ 天:天[date.getDay()], 日期:date.getDate(), 月份:月份[日期.getMonth()], 小时:0(小时), 分钟:appendZero(date.getMinutes()), //dd:dd }; } 函数刷新时钟(){ var now=getTime(); $(“#date').html(now.day+”
“+now.date+”.+now.month); //$('#time').html(now.hour+“:“+now.minute+”+now.dd); $('#time').html(now.hour+“:“+now.minute); setTimeout(函数(){ 刷新时钟(); }, 10000); } 编辑

根据你在rahul回答中的评论

更新行:

hour:appendZero(hour),

小时:追加零((小时+11)%12)+1)


var formatTime=(函数(){
函数addZero(num){
返回(num>=0&&num<10)?“0”+num:num+”;
}
返回函数(dt){
var格式=“”;
if(dt){
var hours24=dt.getHours();
变量小时=((小时24+11)%12)+1;
格式化=[格式化,[addZero(hours),addZero(dt.getMinutes())]。加入(“:”,小时24>11?”下午:“:”上午]。加入(“”;
}
返回格式;
}
})();
警报(格式时间(新日期());

可能重复的可能重复的这是我需要的,但我不需要am/pm,只需要12小时的时间。请参阅更新的代码。评论AP PM逻辑。。。如果有帮助,请接受答案
function getTime() {
  var date = new Date(),
  hour = date.getHours();
 // var dd = "AM";
  var h = hour;
    if (h > 12) {
        h = hour-12;
   //     dd = "PM";
    }
    if (h == 0) {
        h = 12;
    }
  return {
    day: days[date.getDay()],
    date: date.getDate(),
    month: months[date.getMonth()],
    hour: appendZero(h),
    minute: appendZero(date.getMinutes()),
    //  dd: dd  
  };
}


function refreshClock() {
  var now = getTime();
  $('#date').html(now.day + "<br>" + now.date + '. ' + now.month);
 // $('#time').html(now.hour + ":" + now.minute+" "+now.dd);
  $('#time').html(now.hour + ":" + now.minute);
  setTimeout(function() {
    refreshClock();
  }, 10000);
}
var formatTime = (function () {
    function addZero(num) {
        return (num >= 0 && num < 10) ? "0" + num : num + "";
    }

    return function (dt) {
        var formatted = '';

        if (dt) {
            var hours24 = dt.getHours();
            var hours = ((hours24 + 11) % 12) + 1;
            formatted = [formatted, [addZero(hours), addZero(dt.getMinutes())].join(":"), hours24 > 11 ? "pm" : "am"].join(" ");            
        }
        return formatted;
    }
})();

alert(formatTime(new Date()));