Javascript倒计时以及时区和夏时制问题 [英] Javascript countdown and timezone and daylight saving time issues

查看:109
本文介绍了Javascript倒计时以及时区和夏时制问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的团队在JQuery倒计时方面存在重大问题,我们确实需要一些帮助.

Our team are having big issues with the JQuery countdown and we really need some help.

最初,我们有一些执行此操作的ScriptSharp代码

Initially, we had some ScriptSharp code that does this

JQueryCountdownOptions opts = new JQueryCountdownOptions();
opts.Layout = "<ul class=\"group\"> <li>{dn} <span>{dl}</span></li> <li>{hn} <span>{hl}</span></li> <li>{mn} <span>{ml}</span></li> <li>{sn} <span>{sl}</span></li> </ul>";
opts.Until = Number.ParseInt(timeLeft);

jQuery.Select("#countdownclock").Plugin<JQueryCountdown>().Countdown(opts);
jQuery.Select("#countdownclock").Show();
jQuery.Select("#bidBox").RemoveAttr("disabled");

我们注意到这是使用客户端的时钟倒计时的.因此,如果客户决定将时间更改为提前5个小时,那么倒数计时将减少5个小时.

What we noticed is that this uses the client's clock to countdown from. So, if the client decided to change his time to 5 hours ahead then the countdown would be 5 hours off.

为解决此问题,我们引入了更多代码

To fix this we introduced some more code

在视图中:

   $(function () {

        var expires = new Date(@year, @month, @day, @hours, @minutes, @seconds);
        $('#clockDiv').countdown({ until: expires, timeZone: null, serverSync: serverTime, onTick: serverTime, tickInterval: 60 });

        function serverTime() {
            var time = null;
            $.ajax({ url: '/Auction/SyncServerTime',
                async: false, dataType: 'json',
                success: function (result) {
                    time = new Date(result.serverTime);
                }, error: function (http, message, exc) {
                    time = new Date();
                }
            });
            return time;
        }
});

在控制器中

public JsonResult SyncServerTime()
        {
            var result = new JsonResult
            {
                Data = new
                {
                    serverTime = DateTime.Now.ToString("MMM dd, yyyy HH:mm:ss zz")
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
            return result;
        }

此代码可确保无论用户将其时钟设置为倒数计时器如何,都将定期与服务器时间同步.问题解决了.

This code ensures that no matter what the user sets his clock to the countdown timer will periodically sync to the server's time. Problem solved.

唯一的问题是我们还提出了其他问题.

The only issue is that we have come up with other issues.

问题在于,当用户位于不同的时区时,这些用户的倒数时间会有所不同,具体取决于他们所在时区的时区偏移量.我们尝试更改各种参数,但仍然存在问题.更糟糕的是,如果我的时间跨度应用了夏令时,那么对于相同时区的人和不同时区的人来说,事情都会再次出现问题.我们尝试了不同的代码和参数,因此以上内容正是我所做的,并且与尊敬的同事所尝试的不同.我要问的肯定是,某个地方的某人一定有一个要求

The problem is that when users are in different timezones then the countdowns of those users are different depending on the timezone offset that their timezone has. We have tried changing all sorts of parameters and still are having issues. To make matters worse if my timespan straddles a date when daylight saving time is applied then things go awry again, both for those in the same timezone and those in different ones. We have experimented with different code and parameters so the above is just what I did and is different from what my esteemed colleagues tried. What I am asking is surely, someone somewhere out there must have had a requirement to

  1. 编写一个与客户端时间无关并基于服务器时间的倒计时.
  2. 无论用户位于哪个时区,都显示相同的天数,小时数,分钟数和秒数
  3. 对于在此期间由于DST而时间发生变化的用户,与在此期间由于DST而时间没有变化的用户,显示相同的天数,小时,分钟,秒数.
  4. 为由于DST而在此期间时间发生变化的用户显示剩余的实际天,小时,分钟和秒数.
  1. Write a countdown that is independent of client time and based on server time.
  2. Shows the same number of days, hours, minutes, seconds remaining no matter what timezone a user is in
  3. Shows the same number of days, hours, minutes, seconds remaining for a user whose time will change in this period because of DST to user whose time will not change in this period because of DST
  4. Shows the actual number of days, hours, minutes and seconds remaining for a user whose time will change in this period because of DST.

当然,我们不可能是唯一遇到过此问题的人.可以不难.有人知道解决方案吗?

We cannot be the only people who have ever had this issue, surely. It cannot be this hard. Does anyone know a solution?

谢谢

Sachin

推荐答案

我个人还没有处理相同的方案,但是看到日期,时区问题等会自动弹出,引发关于使用某些潜在问题的想法.本地日期对象而不是UTC日期对象.

I haven't dealt with the same scenarios personally, but seeing Date, timezone issues etc. pop up automatically triggers thoughts about some potential issues stemming from the use of local date objects as opposed to UTC date objects.

IMO,如果所有计算,日期序列化仅在UTC空间中有效,情况会变得更好,最后,当涉及到向用户显示日期时,根据情况将其转换为本地或适当的时区.在另一方面,用户输入本地或某个时区的相对输入,并立即将其转换为UTC作为内部表示.这样可以避免在应用程序的不同层/层之间造成各种混乱.

IMO, things are simply better off if all computation, serialization of dates only worked in the UTC space, and finally when it comes to present a date from a user, it is converted to local or appropriate time zone depending on the scenario. On the flip-side, the user enters local or some time zone relative entry, and immediately that is converted to UTC as the internal representation. This avoids all sorts of confusion across different layers/tiers of the app.

这并不是解决您特定问题的真正方法,但是可能需要考虑的事情可能会导致问题.

Its not really a solution to your specific problem, but perhaps something to consider that could lead to one.

这篇关于Javascript倒计时以及时区和夏时制问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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