Cet article décrit la méthode d'affichage du temps de Pékin en temps réel par JavaScript. Partagez-le pour votre référence. Les détails sont les suivants:
Le temps de Pékin s'affiche en temps réel sur cette page. La modification du fuseau horaire peut également être utilisée pour afficher le temps mondial. Le code est le suivant:
Copiez le code comme suit: <script type = "text / javascript">
Date.prototype.strftime = fonction (format) {
var o = {
"M +": this.getMonth () + 1, // mois
"d +": this.getDate (), // jour
"h +": this.gethours (), // heure
"M +": this.getMinutes (), // minute
"S +": this.getSeconds (), // seconde
"Q +": Math.floor ((this.getMonth () + 3) / 3), // Quarter
"S": this.getMilliseconds () // milliseconde
}
if (/ (y +) /. test (format)) {
format = format.replace (regexp. 1 $, (this.getlyear () + ""). substr (4 - regexp. 1.S.Length));
}
pour (var k dans o) {
if (new regexp ("(" + k + ")"). test (format)) {
format = format.replace (regexp. 1 $, regexp. 1 1.Length == 1? O [k]: ("00" + o [k]). substr (("" + o [k]). longueur));
}
}
format de retour;
}
fonction utctolocaltimestring (d, format) {
var TimeOffsetInHours = (new Date (). GetTimeZoneOffset () / 60) + -10;
D.Sethours (D.Gethours () + TimeoffsetInHours);
retour d.strftime (format);
}
</cript>
<cript>
setInterval (function () {var time = utCtolocalTimestring (new Date (), 'yyyy / mm / dd hh: mm: ss'); document.getElementById ("bjtime"). innerHtml = time;}, 1000);
</cript>
<span id = "bjtime"> </span>
J'espère que cet article sera utile à la programmation JavaScript de tous.