如何使用javascript在标签控件中运行时钟的不同时区 [英] How to get different time zones running clocks in label control using javascript

查看:66
本文介绍了如何使用javascript在标签控件中运行时钟的不同时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的JavaScript代码是:



 < script type =   text / javascript> 
function display_c(){
var refresh = 1000 ; // 以毫秒为单位的刷新率
mytime = setTimeout(' display_ct()',refresh)
}

function display_ct(){
var strcount
var x = new 日期()
document .getElementById(' lblDtIST')。innerHTML = x;
tt = display_c();
}
< / script>





html我的asp .net页面的标记是:



 <   body     onload   =  display_ct(); >  
...
< asp:标签 ID = lblDtIST runat = server > < / asp:Label >
...
< / body >





JavaScript在lblDtIST中显示带时区的DateTime。我希望能够使用相同的JavaScript更改DateTime在以下时区显示DateTime:



 lblDtCST.Text = DateTime .UtcNow.AddHours(-6).ToString(  ddd,MMM dd,yyyy hh:mm:ss tt
lblDtMST.Text = DateTime.UtcNow.AddHours( 8 )。ToString( ddd,MMM dd,yyyy hh:mm:ss tt
lblDtGMT.Text = DateTime.UtcNow.AddHours( 0 )。ToString( ddd,MMM dd,yyyy hh:mm:ss tt





谢谢



我尝试了什么:



我试图修改JavaScript但没有帮助。

解决方案

实际上,时区只是定义了从 UTC时间



所以,这是一个想法:



首先,从本地系统获取当前UTC时间: Date.UTC() - JavaScript



你已经正确地做到了;你甚至可以添加本地时间和UTC之间的差异。您只需要对其进行参数化。我能看到的唯一问题是你在服务器端做了些什么。你不应该这样做;这会太慢了。您必须在JavaScript代码中本地处理所有内容,可能在您的计时器事件处理程序中。



您可能需要从服务器端获取的值是小时或时区。当您注册客户端大小的班次时,您可以在处理程序之外创建一个JavaScript变量,并添加事件处理程序的代码,并在那里指定所需的班次(如果您想一次显示三个标签,则需要几个班次)。在处理程序中,可以访问此变量。在您对 .AddHours 的调用中使用其值,就是这样。顺便说一句,这叫做 closure 关闭(计算机编程) - 维基百科,免费百科全书



对不起,如果我没有解决您的问题;我刚用 runat =server回复你的代码片段,这是错误的。如果事情不清楚,请首先澄清你的问题,这不太清楚 - 请看我对这个问题的评论。



-SA

Hi,

My JavaScript Code is:

<script type="text/javascript">
        function display_c() {
            var refresh = 1000; // Refresh rate in milli seconds
            mytime = setTimeout('display_ct()', refresh)
        }

        function display_ct() {
            var strcount
            var x = new Date()
            document.getElementById('lblDtIST').innerHTML = x;
            tt = display_c();
        }
    </script>



html Markup for my asp .net page is:

<body onload="display_ct();">
...
<asp:Label ID="lblDtIST" runat="server"></asp:Label>
...
</body>



The JavaScript displays DateTime with Time Zone in lblDtIST. I want to be able to show DateTime in the following Time Zones using the same JavaScript with changing DateTime:

lblDtCST.Text = DateTime.UtcNow.AddHours(-6).ToString("ddd, MMM dd, yyyy hh:mm:ss tt")
lblDtMST.Text = DateTime.UtcNow.AddHours(8).ToString("ddd, MMM dd, yyyy hh:mm:ss tt")
lblDtGMT.Text = DateTime.UtcNow.AddHours(0).ToString("ddd, MMM dd, yyyy hh:mm:ss tt")



Thanks

What I have tried:

I have tried to modify the JavaScript but of no help.

解决方案

Indeed, time zones just define number of hours added to or subtracted from UTC time.

So, here is the idea:

First, get the current UTC time from the local system: Date.UTC() — JavaScript.

You already do it correctly; you even add the difference between the local time and UTC. You only need to parametrize it. The only problem I can see is that your do something on the server side. You should not do it; it would be way too slow. You have to handle everything locally in your JavaScript code, probably in your timer event handler.

All you may possibly need from the server side is the value of the shift in hours, or the time zone. When you register your client-size shift, you can create a JavaScript variable outside of the handler and the code adding the event handler and prescribe required shift on hours there (of several shifts, if you want to show three labels at a time). In a handler, this variable will be accessible. Use its value in your call to .AddHours, that's all. This is called closure, by the way: Closure (computer programming) — Wikipedia, the free encyclopedia.

Sorry if I didn't address your concern; I just responded to your code fragment with runat="server", which is wrong thing. If something is unclear, please first clarify your question, which is not quite clear — please see my comment to the question.

—SA


这篇关于如何使用javascript在标签控件中运行时钟的不同时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆