บทความนี้อธิบายวิธีการแสดงเวลาปักกิ่งแบบเรียลไทม์โดย JavaScript แบ่งปันสำหรับการอ้างอิงของคุณ รายละเอียดมีดังนี้:
เวลาปักกิ่งจะแสดงแบบเรียลไทม์ในหน้านี้ การเปลี่ยนเขตเวลายังสามารถใช้เพื่อแสดงเวลาโลก รหัสมีดังนี้:
คัดลอกรหัสดังนี้: <script type = "text/javascript">
date.prototype.strftime = function (รูปแบบ) {
var o = {
"M+": this.getMonth ()+1, // เดือน
"d+": this.getdate (), // วัน
"H+": this.getHours (), // ชั่วโมง
"M+": this.getMinutes (), // นาที
"S+": this.getSeconds (), // วินาที
"Q+": Math.floor ((this.getMonth ()+3)/3), // ไตรมาส
"s": this.getmilliseconds () // มิลลิวินาที
-
if (/(y+)/. ทดสอบ (รูปแบบ)) {
format = format.replace (regexp. $ 1, (this.getlyear ()+""). substr (4 - regexp. $ 1. ความยาว));
-
สำหรับ (var k ใน o) {
if (ใหม่ regexp ("(" + k + ")"). ทดสอบ (รูปแบบ)) {
format = format.replace (regexp. $ 1, regexp. $ 1.length == 1? o [k]: ("00"+ o [k]). substr ((""+ o [k]). ความยาว);
-
-
รูปแบบการส่งคืน;
-
ฟังก์ชั่น utctolocaltimestring (d, รูปแบบ) {
var TimeOffsetInhours = (วันที่ใหม่ (). getTimeZoneOffset ()/60) + -10;
D.Sethours (D.GetHours () + TimeOffsetInhours);
ส่งคืน d.strftime (รูปแบบ);
-
</script>
<script>
setInterval (function () {var time = utctolocaltimestring (วันที่ใหม่ (), 'yyyy/mm/dd hh: mm: ss'); document.getElementById ("bjtime"). innerhtml = เวลา;}, 1,000);
</script>
<span id = "bjtime"> </span>
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม JavaScript ของทุกคน