【问题标题】:Multiple Countdown with timezone多个时区倒计时
【发布时间】:2020-05-27 15:11:35
【问题描述】:

我需要在带有时区的单个页面中使用多个倒计时。我找到了Final Countdown

我找到了我需要的代码。但无法让它在正确的时区工作。

    $(function(){
 $('[data-countdown]').each(function() {
   var $this = $(this), finalDate = $(this).data('countdown');
   $this.countdown(finalDate, function(event) {
     $this.html(event.strftime('%H:%M:%S'));
   });
 });
});

这适用于多个倒计时,效果很好..

这是一个带有单个倒计时的时区。

var nextYear = moment.tz("2020-05-29 00:00", "America/Sao_Paulo");

$('#clock').countdown(nextYear.toDate(), function(event) {
  $(this).html(event.strftime('%D days %H:%M:%S'));
});

我怎样才能让第一个工作在适当的时区.. 非常感谢

【问题讨论】:

    标签: javascript jquery countdown


    【解决方案1】:

    只需使用时刻时区功能将带有时区的日期传递给每个倒计时。

    这就是你要找的东西?

    $(function(){
        $('[data-countdown]').each(function() {
            var $this = $(this), finalDate = $(this).data('countdown');
            $this.countdown(moment.tz(finalDate, "America/Sao_Paulo").toDate(), function(event) {
                $this.html(event.strftime('%H:%M:%S'));
            });
        });
    });
    

    要获取用户本地时区,您可以尝试

    console.log(Intl.DateTimeFormat().resolvedOptions().timeZone); // America/Sao_Paulo
    //or with moment
    console.log(moment.tz.guess()) // America/Sao_Paulo
    

    确保您导入 moment cdn 库

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.min.js"></script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      相关资源
      最近更新 更多