2013-05-13 13 views
5

Desidero visualizzare l'ora nel formato 12 ore modificando il seguente codice. Ho provato varie tecniche ma senza fortuna, spero di trovare la soluzione da voi ragazzi.Tempo di visualizzazione in formato 12 ore in JavaScript

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

possibile duplicato del [Sostituire t militare ime al tempo normale con Javascript] (http://stackoverflow.com/questions/14415618/replace-military-time-to-normal-time-with-javascript) –

risposta

6

EDIT

sulla base di osservazioni in risposta di Rahul ...

aggiornare la linea:

hour: appendZero(hour),

a

hour: appendZero(((hour + 11) % 12) + 1)Live Demo


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

grazie Che ha fatto il lavoro :) –

4

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

Questo è quello di cui ho bisogno ma non ho bisogno di am/pm, solo 12 ore. –

+0

Vedere il codice aggiornato. ha commentato la logica AP PM ... Se ha aiutato si prega di accettare la risposta –

+0

grazie per gli sforzi rahul –

Problemi correlati