Este artigo descreve o método de exibir o tempo de Pequim em tempo real por JavaScript. Compartilhe para sua referência. Os detalhes são os seguintes:
O tempo de Pequim é exibido em tempo real nesta página. Alterar o fuso horário também pode ser usado para exibir o tempo mundial. O código é o seguinte:
Copie o código da seguinte forma: <script type = "text/javascript">
Date.prototype.strftime = function (formato) {
var o = {
"M+": this.getmonth ()+1, // mês
"D+": this.getdate (), // dia
"h+": this.gethours (), // hora
"M+": this.getminutes (), // minuto
"S+": this.getSeconds (), // Segundo
"q+": math.floor ((this.getmonth ()+3)/3), // trimestre
"S": this.getMillisEconds () // milissegundos
}
if (/(y+)/. teste (formato)) {
formato = format.Replace (regexp. $ 1, (this.getlyear ()+""). substr (4 - regexp. $ 1.Length));
}
para (var k em o) {
if (novo regexp ("(" + k + ")"). test (formato)) {
formato = format.Replace (regexp. $ 1, regexp. $ 1.Length == 1? O [k]: ("00"+ o [k]). substr ((""+ o [k]). comprimento));
}
}
Formato de retorno;
}
função utctolocaltimestring (d, formato) {
var timeOffSetInhours = (new Date (). getTimeZoneOffSet ()/60) + -10;
D.Sethours (D.Gethours () + TimeOffSetInhours);
retorno d.strftime (formato);
}
</script>
<Cript>
setInterval (function () {var time = utctolocaltimestring (new Date (), 'yyyy/mm/dd hh: mm: ss'); document.getElementById ("bjtime"). innerhtml = time;}, 1000);
</script>
<span id = "bjtime"> </span>
Espero que este artigo seja útil para a programação JavaScript de todos.