Questa funzione converte il tempo di 12 ore formato, crediti per un collaboratore su Stack Overflow per questa funzione:Come si passa il valore di un tempo di tipo di input a un oggetto Date?
JS
function ampm(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // 0 should be 12
minutes = minutes < 10 ? '0'+minutes : minutes; // if minutes less than 10, add a 0 in front of it ie: 6:6 -> 6:06
var strTime = hours + ':' + minutes + ' ' + ampm;
document.getElementById('time').value = strTime;
return strTime;
}
////This is how the value of the time input is supposed to be displayed in 12 hr format
_("display_time").innerHTML = ampm(new Date());
HTML
<!--This is the input field where a user selects a time-->
<input id="time" placeholder="Time" type="time" name="time" />
<!--This is where the value of the time input is supposed to be displayed in 12 hr format-->>
<span id="display_time"></span>
mia domanda è come fare Ricevo il valore del campo di immissione del tempo da visualizzare sul tag span in formato 12 ore. Questo codice è semi-funzionante.
Visualizza l'ora nel formato 12 ore ma visualizza solo l'ora corrente. Il diagramma di flusso sarebbe qualcosa di simile, l'utente seleziona l'input temporale nel tempo -> qualche JS per convertire in formato 12 ore -> visualizza come formato 12 ore nel tag span. Qualche consiglio o suggerimento?
Per essere onesti, sembrerebbe come si sta complicando la vita. Ci sono un sacco di domande StackOverflow riguardanti gli oggetti di data e ora che propongono soluzioni più semplici. Devi solo cercare un po '. – rottenoats
Come pensate di consentire all'utente di "selezionare l'input in tempo"? Che formato è quel tempo (stringa? Ora? Timestamp?) –
Vedere questo http://stackoverflow.com/questions/4904667/html-how-do-i-insert-dynamic-date-in-webpage – aSoler