如何制作一个显示英国时间的时钟,但整个应用程序都是印度时间

How to make a clock showing UK time but the whole application is working in India Time

本文关键字:时间 应用程序 时钟 何制作 一个 显示 英国      更新时间:2023-09-26

我有以下代码-

<!DOCTYPE html>
<html>
<head>
<script>
function startTime() {
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML = h+":"+m+":"+s;
    var t = setTimeout(function(){startTime()},500);
}
function checkTime(i) {
    if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>

这段代码引用自w3schools。但是这个代码占用了我们系统的时间。但我想要一个javascript或服务器端方法在PHP中,我可以显示英国时间在我的页面的顶部。如果我在配置文件中将时区更改为英国时间,但除了页面顶部的时钟外,我希望所有地方都是印度时间,这很容易做到。

直接使用getUTCDatetoUTCString

var d = new Date(); // local date
var n = d.getUTCDate(); // utc date

获取完整日期字符串

var d = new Date(); 
var n = d.toUTCString();

Demo --> http://jsfiddle.net/gPRBH/

使用getUTCDate()和相关的getUTC…()方法访问基于UTC时间的时间,然后转换