帮助处理重复的 JavaScript 倒计时和时区

发布于 2024-10-14 22:06:33 字数 2572 浏览 2 评论 0原文

目标: 创建下一个可用直播的倒计时。

详细信息: 我们每周直播六次(太平洋标准时间)。
1. 周日上午 8:00
2. 周日上午 10:00
3. 周日中午 12:00
4. 周日下午 6:30
5. 周三晚上 7:00
6. 周六上午 10:00

我的方法: 最初,我检查今天是星期几以及时间,然后创建下一个流的倒计时。这对于我们的时区 (PST) 以及任何相对接近的时区来说效果很好。

问题: 首先检查日期打破了我对位于时区的人们的方法,这些时区使他们比我们早一天。例如,如果来自伦敦的某人在周四凌晨 2:30(格林威治标准时间)检查该网站,我的代码将开始倒计时到周六上午 10:00 直播,而不是周三晚上 7:00(太平洋标准时间),后者将于 30 点开始分钟

我确信我所做的事情可以得到清理和改进,请告诉我如何做。

我的代码:

// Countdown
function countDown( addDay, hour, min ) {
    var newCount = new Date(); 
    newCount = new Date(newCount.getFullYear(), newCount.getMonth(), newCount.getDate() + addDay, hour, min); 
    $('#service_countdown').countdown({until: $.countdown.UTCDate(-8, newCount)}); 
}

var day_ref, day, addDay, hour, min;
addDay = 0;
hour = 0;
min = 0;
day = {};

day_ref = new Date();
day = {"day" : day_ref.getDay(), "hour" : day_ref.getHours(), "min" : day_ref.getMinutes()};

// It's Sunday
if ( day.day === 0 ) {

    if ( day.hour < 8 ) {
          hour = 8;
        countDown(addDay, hour, min);
    } 
    else if ( day.hour < 10 ) {
          hour = 10;
        countDown(addDay, hour, min);
    } 
    else if ( day.hour < 12 ) {
          hour = 12;
        countDown(addDay, hour, min);
    }
    else if ( day.hour <= 18 && day.min < 30 ) {
          hour = 18;
           min = 30;
        countDown(addDay, hour, min);
    } 
    else {
        addDay = 3;
          hour = 19;
        countDown(addDay, hour, min);
    }
} 
// It's Monday
else if ( day.day === 1 ) {
    addDay = 2;
      hour = 19;
    countDown(addDay, hour, min);
} 
// It's Tuesday
else if ( day.day === 2 ) {
    addDay = 1;
      hour = 19;
    countDown(addDay, hour, min);
} 
// It's Wednesday
else if ( day.day === 3) {

    if ( day.hour < 19 ) {
          hour = 19;
        countDown(addDay, hour, min);
    } else {
        addDay = 3;
          hour = 10;
        countDown(addDay, hour, min);
    }
} 
// It's Thursday
else if ( day.day === 4 ) {
    addDay = 2;
      hour = 10;
    countDown(addDay, hour, min);
} 
// It's Friday
else if ( day.day === 5 ) {
    addDay = 1;
      hour = 10;
    countDown(addDay, hour, min);
} 
// All that's left is Saturday
else {

    if ( day.hour < 10 ) {
          hour = 10;
        countDown(addDay, hour, min);
    } else {
        addDay = 1;
          hour = 8;
        countDown(addDay, hour, min);
    }
}

提前感谢您的帮助!

Goal:
To create a countdown to our next available live stream.

Details:
We live stream six times a week all (PST).
1. Sunday at 8:00 a.m.
2. Sunday at 10:00 a.m.
3. Sunday at 12:00 p.m.
4. Sunday at 6:30 p.m.
5. Wednesday at 7:00 p.m.
6. Saturday at 10:00 a.m.

My approach:
Originally I check what day it is and what time it is then create the countdown to the next stream. This works fine for our timezone (PST) and any that are relatively close.

Problem:
Checking first the day breaks my approach for people in timezones that put them a day ahead of us. For example if someone from London checks the site on Thursday at 2:30 a.m. (GMT) my code would start the countdown to the Saturday 10:00 a.m. stream instead of the Wednesday at 7:00 p.m. (PST) which would start in 30 mins.

I'm sure what I have done can be cleaned up and improved, tell me how.

My code:

// Countdown
function countDown( addDay, hour, min ) {
    var newCount = new Date(); 
    newCount = new Date(newCount.getFullYear(), newCount.getMonth(), newCount.getDate() + addDay, hour, min); 
    $('#service_countdown').countdown({until: $.countdown.UTCDate(-8, newCount)}); 
}

var day_ref, day, addDay, hour, min;
addDay = 0;
hour = 0;
min = 0;
day = {};

day_ref = new Date();
day = {"day" : day_ref.getDay(), "hour" : day_ref.getHours(), "min" : day_ref.getMinutes()};

// It's Sunday
if ( day.day === 0 ) {

    if ( day.hour < 8 ) {
          hour = 8;
        countDown(addDay, hour, min);
    } 
    else if ( day.hour < 10 ) {
          hour = 10;
        countDown(addDay, hour, min);
    } 
    else if ( day.hour < 12 ) {
          hour = 12;
        countDown(addDay, hour, min);
    }
    else if ( day.hour <= 18 && day.min < 30 ) {
          hour = 18;
           min = 30;
        countDown(addDay, hour, min);
    } 
    else {
        addDay = 3;
          hour = 19;
        countDown(addDay, hour, min);
    }
} 
// It's Monday
else if ( day.day === 1 ) {
    addDay = 2;
      hour = 19;
    countDown(addDay, hour, min);
} 
// It's Tuesday
else if ( day.day === 2 ) {
    addDay = 1;
      hour = 19;
    countDown(addDay, hour, min);
} 
// It's Wednesday
else if ( day.day === 3) {

    if ( day.hour < 19 ) {
          hour = 19;
        countDown(addDay, hour, min);
    } else {
        addDay = 3;
          hour = 10;
        countDown(addDay, hour, min);
    }
} 
// It's Thursday
else if ( day.day === 4 ) {
    addDay = 2;
      hour = 10;
    countDown(addDay, hour, min);
} 
// It's Friday
else if ( day.day === 5 ) {
    addDay = 1;
      hour = 10;
    countDown(addDay, hour, min);
} 
// All that's left is Saturday
else {

    if ( day.hour < 10 ) {
          hour = 10;
        countDown(addDay, hour, min);
    } else {
        addDay = 1;
          hour = 8;
        countDown(addDay, hour, min);
    }
}

Thanks in advance for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2024-10-21 22:06:33

我最终只是一直在计算服务器端。在 bob-the-destroyer 的帮助下,考虑到要求,工作 php 非常紧凑。

<?php

$schedule = array(
    'this Sunday 8am',
    'this Sunday 10am',
    'this Sunday 12pm',
    'this Sunday 6:30pm',
    'this Wednesday 7pm',
    'this Saturday 10am'
    );

$current_time = strtotime('now');
foreach ($schedule as &$val) {
    $val = strtotime($val);
    // fix schedule to next week if time resolved to the past
    if ($val - $current_time < 0) $val += 604800; 
    }
sort($schedule);
$countdown = $schedule[0] - $current_time;

?>

<script type="text/javascript">
    var myTime = <?php echo $countdown; // just personally prefer full tags ?>;
    $('#countdown').countdown({ until: myTime}); 
</script>

I ended up just doing all the time calculating server side. With the help of bob-the-destroyer the working php is quite compact given the requirements.

<?php

$schedule = array(
    'this Sunday 8am',
    'this Sunday 10am',
    'this Sunday 12pm',
    'this Sunday 6:30pm',
    'this Wednesday 7pm',
    'this Saturday 10am'
    );

$current_time = strtotime('now');
foreach ($schedule as &$val) {
    $val = strtotime($val);
    // fix schedule to next week if time resolved to the past
    if ($val - $current_time < 0) $val += 604800; 
    }
sort($schedule);
$countdown = $schedule[0] - $current_time;

?>

<script type="text/javascript">
    var myTime = <?php echo $countdown; // just personally prefer full tags ?>;
    $('#countdown').countdown({ until: myTime}); 
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文