简体   繁体   English

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

[英]Display Time in 12 Hour Format in JavaScript

I want to display Time in 12 hour format by altering the following code. 我想通过更改以下代码以12小时格式显示时间。 i Tried Various Techniques but no luck, hope to find The solution from u guys . 我尝试了各种技术,但没有运气,希望找到你们的解决方案。

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

EDIT 编辑

Based on your comments in rahul's answer... 根据您在rahul的回答中的评论......

Update the line: 更新行:

hour: appendZero(hour),

to

hour: appendZero(((hour + 11) % 12) + 1) Live Demo hour: appendZero(((hour + 11) % 12) + 1) 现场演示


Live Demo 现场演示

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())); 

DEMO DEMO

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以 12 小时制显示当前时间? - Javascript - Display current time in 12 hour time? - Javascript JavaScript和C#中的12小时时间格式比较 - 12 Hour Time Format Comparison in JavaScript and C# 来自 12 小时格式日期和时间字符串的 JavaScript 新日期 - JavaScript New Date from a 12 hour format date and time string JavaScript以12小时格式随机生成时间 - JavaScript random generate time in 12-hour format 如何使用javascript将时间从24小时格式转换为12小时格式? - How to convert time from 24 hour format to 12 hour format using javascript? 为 javascript 设置 12 小时时间 - Setting a 12 hour Time for javascript JavaScript 确定浏览器是否使用 12 小时或 24 小时格式显示时间输入 - JavaScript to determine if browser is displaying the time input using a 12-hour or 24-hour format Javascript -- 检测用户的区域设置是否设置为使用 12 小时或 24 小时时间格式 - Javascript -- Detect if user's locale are set to use 12-hour or 24-hour time format Trying to write a function to display the time in a 12 hour format but the HTML is not displaying the time and the function returns null - Trying to write a function to display the time in a 12 hour format but the HTML is not displaying the time and the function returns null 如何在高图表中显示从当前时间(hh:mm)12小时格式开始的最近五个小时的时间? - how to display the last five hours time from current time(hh:mm ) 12 hour format in high charts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM