简体   繁体   English

如何在网页上显示其他时区的当前时间?

[英]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.

相关问题 如何在我的网页上每分钟从所选时区更新时间? - How can I update the time from chosen timezone every minute on my webpage? 如何在 JavaScript 的页面上显示当前时区、日期和时间? - How can I show the current timezone, date, and time on the page in JavaScript? 如何从网页上的嵌入式视频中获取当前播放时间? - How can I get the current playing time from an embedded video on a webpage? 将PageDown编辑器嵌入到网页中时,如何阻止它进行实时转换? - When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time? 如何获取访问我网站的用户的当前时间/时区? - How to get the current time/timezone of the user visiting my website? 如何使用 moment.js 使用时区偏移量获取当前时间? - How can I get the current time using timezone offset using moment.js? 如何在 javascript 中选择不同的时区? - How can I choose a different timezone in javascript? 如何将内容从博客中提取到我的网页上? - How can I pull content from blogger onto my webpage? 如何从我的网页运行本地服务器脚本? - how can i run a local server script from my webpage? 如何从网页中删除一些JavaScript? - How can I remove a some javascript from my webpage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM