[英]How can I show the current time from a different timezone on my webpage?
I want to show the time from the timezone chosen by me on my webpage. 我想在我的网页上显示我选择的时区的时间。 I found the way to show the current time as it's presented here http://www.webestools.com/ftp/ybouane/scripts_tutorials/javascript/date_time/date_time.html :
我在http://www.webestools.com/ftp/ybouane/scripts_tutorials/javascript/date_time/date_time.html中找到了显示当前时间的方法:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Display Date and Time in Javascript</title>
<script type="text/javascript" src="date_time.js"></script>
</head>
<body>
<span id="date_time"></span>
<script type="text/javascript">window.onload = date_time('date_time');</script>
</body>
</html>
and JS: 和JS:
function date_time(id)
{
date = new Date;
year = date.getFullYear();
month = date.getMonth();
months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'Jully', 'August', 'September', 'October', 'November', 'December');
d = date.getDate();
day = date.getDay();
days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
h = date.getHours();
if(h<10)
{
h = "0"+h;
}
m = date.getMinutes();
if(m<10)
{
m = "0"+m;
}
s = date.getSeconds();
if(s<10)
{
s = "0"+s;
}
result = ''+days[day]+' '+months[month]+' '+d+' '+year+' '+h+':'+m+':'+s;
document.getElementById(id).innerHTML = result;
setTimeout('date_time("'+id+'");','1000');
return true;
}
but if I understand it correctly - it always shows the current time from the timezone of the viewer of the page. 但如果我正确理解-它总是显示页面查看者所在时区的当前时间。 So when someone logs in there from eg Australia - he will see his result, and the guy from Poland will see sth completely different.
因此,当某人从例如澳大利亚登录那里时-他会看到他的结果,而来自波兰的那个人将会看到完全不同的东西。 I would like to present on the webpage the current time in fixed timezone, let's say eg in New York.
我想在网页上显示固定时区的当前时间,例如在纽约。 How should I modify this script to get the expected results?
我应该如何修改此脚本以获得预期的结果? Thanks!
谢谢!
You can use moment-timezone to convert time to differnt timezone 您可以使用moment-timezone将时间转换为不同的时区
Here Example: 这里的例子:
function toTimeZone(time, zone) {
var format = 'YYYY/MM/DD HH:mm:ss ZZ';
return moment(time, format).tz(zone).format(format);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.