仅在此javascript代码中以HH:MM显示时间

时间:2014-09-24 13:52:42

标签: javascript

以下JS以HH:MM:SS格式显示时间,而我需要它来显示HH:仅限MM

  setInterval(function() {
    var d = new Date();
    var t = d.getTime();
    var interval = 5*60*1000;
    var last = t - t % interval;
    var next = last + interval + 10*60000;
    d.setTime(next);
    var time = d.toLocaleTimeString();
    $(".clock").html(time);
  }, 1000);

关于如何实现这一点的任何想法?

小提琴:http://jsfiddle.net/7z9boag8/

4 个答案:

答案 0 :(得分:0)

试试这个:

var time = d.toLocaleTimeString().match(/(\d+:\d+):\d+( \w{2})*/);
var time = time[1] + (time[2] ? time[2] : "");

答案 1 :(得分:0)

对不起。我不编辑你的代码。我只是给你另一个程序

String.prototype.toHHMMSS = function () {
    var sec_num = parseInt(this, 10); // don't forget the second param
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    var time    = hours+':'+minutes+':'+seconds;
    return time;
}

第二个公式是

function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

function yourTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    // add a zero in front of numbers<10
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
    t = setTimeout(function () {
        startTime()
    }, 500);
}
yourTime();

答案 2 :(得分:0)

可以使用getHours()和getMinuets()方法。

例如: http://jsfiddle.net/0todu2y7/

jQuery(function($) {
setInterval(function() {
var d = new Date();
var t = d.getTime();
var interval = 5*60*1000;
var last = t - t % interval;
var nextt = last + interval + 5*60000;
d.setTime(nextt);
var hours = d.getHours();
var min = d.getMinutes();
  $(".clock").html(hours+":"+min);
  }, 1000);
});

答案 3 :(得分:-2)

setInterval(function() {
    var d = new Date();
    var t = d.getTime();
    var interval = 5*60*1000;
    var last = t - t % interval;
    var next = last + interval + 10*60000;
    d.setTime(next);
    var time = d.toLocaleTimeString().split(':')
    time.pop()
    time.join(':')
    $(".clock").html(time);
  }, 60000);

我不认为你每秒都会运行tins功能。你可以在一分钟内完成一次。