Javascript显示日期和时间在我的网站使用UTC考虑用户的时区在Javascript
Javascript display date and time in my website using UTC to consider users timezone in javascript
我想在我的网站上有一个定时器,就像一个数字时钟。它会有一个日期和时间。我用下面的代码来做:
var clockID = 0;
function UpdateClock() {
if(clockID) {
clearTimeout(clockID);
clockID = 0;
}
var tDate = new Date();
var in_hours = tDate.getHours()
var in_minutes=tDate.getMinutes();
var in_seconds= tDate.getSeconds();
if(in_minutes < 10)
in_minutes = '0'+in_minutes;
if(in_seconds<10)
in_seconds = '0'+in_seconds;
if(in_hours<10)
in_hours = '0'+in_hours;
document.getElementById('theTime').innerHTML = ""
+ in_hours + ":"
+ in_minutes + ":"
+ in_seconds;
clockID = setTimeout("UpdateClock()", 1000);
}
function StartClock() {
clockID = setTimeout("UpdateClock()", 500);
}
function KillClock() {
if(clockID) {
clearTimeout(clockID);
clockID = 0;
}
}
但是这段代码显示的是当前的计算机时间,所以时间不适合我的时区。我还需要在我的代码中添加什么,以便它根据我的时区显示日期和时间?如果日期是夏令时,它也会显示夏令时的确切时间。
如果您只需要使用JS,请查看
添加或减去javascript日期的时区差异
例如DEMO
var clockID;
var yourTimeZoneFrom = -7.00; //time zone value where you are at
var d = new Date();
//get the timezone offset from local time in minutes
var tzDifference = yourTimeZoneFrom * 60 + d.getTimezoneOffset();
//convert the offset to milliseconds
var offset = tzDifference * 60 * 1000;
function UpdateClock() {
var tDate = new Date(new Date().getTime()+offset);
var in_hours = tDate.getHours()
var in_minutes=tDate.getMinutes();
var in_seconds= tDate.getSeconds();
if(in_minutes < 10)
in_minutes = '0'+in_minutes;
if(in_seconds<10)
in_seconds = '0'+in_seconds;
if(in_hours<10)
in_hours = '0'+in_hours;
document.getElementById('theTime').innerHTML = ""
+ in_hours + ":"
+ in_minutes + ":"
+ in_seconds;
}
function StartClock() {
clockID = setInterval(UpdateClock, 500);
}
function KillClock() {
clearTimeout(clockID);
}
window.onload=function() {
StartClock();
}
在JS中你可以得到当前的时区偏移量。您需要将偏移量调整到所需的时区。
<div id="theTime"></div>
<script>
var clockID = 0;
var requiredTimeZone = 360; // CST (+6:00 hrs)
function UpdateClock() {
if(clockID) {
clearTimeout(clockID);
clockID = 0;
}
var tDate = new Date();
var calculatedTime = tDate.getTime() + (tDate.getTimezoneOffset() * 60000) - (requiredTimeZone * 60000);
tDate.setTime(calculatedTime);
var in_hours = tDate.getHours()
var in_minutes=tDate.getMinutes();
var in_seconds= tDate.getSeconds();
if(in_minutes < 10)
in_minutes = '0'+in_minutes;
if(in_seconds<10)
in_seconds = '0'+in_seconds;
if(in_hours<10)
in_hours = '0'+in_hours;
document.getElementById('theTime').innerHTML = ""
+ in_hours + ":"
+ in_minutes + ":"
+ in_seconds;
clockID = setTimeout("UpdateClock()", 1000);
}
function StartClock() {
clockID = setTimeout("UpdateClock()", 500);
}
function KillClock() {
if(clockID) {
clearTimeout(clockID);
clockID = 0;
}
}
StartClock();
</script>
将当前浏览器的时区偏移量添加到浏览器时间中,然后减去所需的时区偏移量以获得所需的时间。
您所需的时区需要以某种方式传达给浏览器,以使其工作
相关文章:
- 将日期和时间转换为UTC格式的日期-Javascript
- Javascript格式UTC日期
- javascript/jquery将utc转换为短格式的本地时间
- 如何在时间标签和相同的类中以UTC格式重新格式化ISO 8601时间戳,并通过JavaScript根据用户区域设置和时区
- 根据浏览器位置,用JavaScript将输入的日期转换为UTC
- 如何使用 javascript 获取 UTC 格式的日期,而不考虑当前系统日期
- 如何在javascript中获取正确的UTC时间
- 如何使用javascript在网络上仅将UTC时间戳转换为本地时间?从 http://openweathermap.or
- 如何使用javascript以操纵的UTC格式获取当前日期
- javascript使jquery倒计时与UTC时间一起工作
- 使用JavaScript将时间戳UTC转换为IST
- 将UTC日期从Javascript发送到MVC
- 转换&用javascript将mongodb的UTC格式显示为EST格式
- 使用Mysql UNIX_TIMESTAMP()与JAVASCRIPT Date.UTC()获得不同的结果
- PHP 相当于 JavaScript 中的 Date.UTC 函数
- Javascript-UTC日期到本地日期
- 如何将毫秒转换为Javascript UTC日期
- Javascript UTC match RegExp
- 移动浏览器中的Javascript UTC日期问题
- 将javascript UTC日期存储在mysql中.php