2012-06-18 65 views
-1

我想重新格式化數字時鐘計時器。當時間是3:15(下午)時,jquery timmer將在下午15:15和12:00(上午)向我顯示24:00(上午)。而不是24小時格式化,我們可以將其設置爲12小時格式?jquery時間格式

例子:

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

請幫助我。

回答

0

像這樣?

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

之前的「領導零」部分。另外,如果你不想在幾小時內引導零,「3:15」而不是「03:15」,只需刪除該行代碼即可。

+0

謝謝,但你能讓我知道我應該在我的jQuery中添加你的代碼嗎?我真的很感激,如果你上傳jsfiddle的代碼或給我更新完整的代碼,因爲我不擅長jquery。 –

+0

我試圖複製你的代碼,並通過我的jQuery代碼的不同行。經過三四次嘗試,我獲得了成功。非常感謝您的幫助。對此,我真的非常感激。 –