Jquery时间格式化

时间:2012-06-18 10:55:37

标签: jquery timer

我想重新格式化数字时钟计时器。当时间是3:15(下午)时,jquery timmer给我看15:15 pm和12:00(am)它显示我24:00(am)。而不是24小时格式化,我们可以将其设置为12小时格式化吗?

示例:

var maxnumhours = 11;
var maxnummins = 59;
var maxnumsecs = 60;
var maxmilisecs = 999;

$(document).ready(function() {
  updateClock();
  setInterval('updateClock()', 250 );
});

function hexifyWithZeroLead(tohex){
    var rtn = tohex.toString(16);
    return ( rtn.length == 1 ? "0" : "" ) + rtn;
}

function updateClock ( )
{
  var currentTime = new Date ( );
  var currentHours = currentTime.getHours();
  var currentMinutes = currentTime.getMinutes();
  var currentSeconds = currentTime.getSeconds();
  var currentMiliSeconds = currentTime.getMilliseconds();
  var rounded = currentSeconds + (currentMiliSeconds / maxmilisecs);

  rednum = (Math.round(255 * ((currentHours) / maxnumhours)));
  rednum100 = (Math.round(100 * ((currentHours) / maxnumhours)));
  greennum = (Math.round(255 * ((currentMinutes) / maxnummins)));
  greennum100 = (Math.round(100 * ((currentMinutes) / maxnummins)));
  bluenum = (Math.round(255 * ((rounded) / maxnumsecs)));
  bluenum100 = (Math.round(100 * ((rounded) / maxnumsecs)));

  redhex = hexifyWithZeroLead(rednum);
  greenhex = hexifyWithZeroLead(greennum);
  bluehex = hexifyWithZeroLead(bluenum);

  var hex = "#" + redhex + greenhex + bluehex;
  var fullredhex = "#"+redhex+"0000";
  var fullgreenhex = "#00"+greenhex+"00";
  var fullbluehex = "#0000"+bluehex;

  jQuery("#red_display").html(redhex);
  jQuery("#green_display").html(greenhex);
  jQuery("#blue_display").html(bluehex);

  leftpos = (rednum100 * 0.01 * 575) + 25;
  jQuery('#red_display').animate({left: leftpos}, 200);
  jQuery('#red_display').css('background-color',fullredhex);

  leftpos = (greennum100 * 0.01 * 575) + 25;
  jQuery('#green_display').animate({left: leftpos}, 200);
  jQuery('#green_display').css('background-color',fullgreenhex);

  leftpos = (bluenum100 * 0.01 * 575) + 25;
  jQuery('#blue_display').animate({left: leftpos}, 200);
  jQuery('#blue_display').css('background-color',fullbluehex);



  // Leading Zeros
  currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
  jQuery("#clock").html("<span id='hours'>"+ currentHours + "</span>:<span id='minutes'>" + currentMinutes + "</span>:<span id='seconds'>" + currentSeconds + '</span>');
  jQuery("#hex").html(hex);
}

请帮帮我。

1 个答案:

答案 0 :(得分:0)

这样的东西?

var suffix = 'am';
if(currentHours > 12) {
    currentHours -= 12;
    suffix = 'pm';
}

在“领先的零”部分之前。此外,如果你不想在数小时前导零,“3:15”而不是“03:15”,只需删除那行代码。