1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5 <title>设置显示不同时区时间</title> 6 </head> 7 8 <body> 9 <p id="showdate"></p> 10 11 <!--引入jQuery插件 --> 12 <script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.min.js"></script> 13 14 <script> 15 // 时间设置 16 var timezone = 8; // 时区 : 东区时区1~12 西区时区-1~-12 17 var date; 18 19 date = getLocalTime(timezone) 20 .toString() 21 .split("GMT+")[0] 22 .toString(); 23 $("#showdate").html(date); 24 25 function getLocalTime(i) { 26 if (typeof i !== "number") { 27 return new Date(); 28 } 29 var d = new Date(); 30 var len = d.getTime(); 31 var offset = d.getTimezoneOffset() * 60000; 32 var utcTime = len + offset; 33 return new Date(utcTime + 3600000 * i); 34 } 35 36 // 时间设置end 37 38 window.onload = function() { 39 // 调用时间 40 var setdate = setInterval(() => { 41 date = getLocalTime(timezone) 42 .toString() 43 .split("GMT+")[0] 44 .toString(); 45 $("#showdate").html(date); 46 }, 1000); 47 48 // 调用时间end 49 }; 50 </script> 51 </body> 52 </html>