这个JS时间码可以用吗?我可以让它变得更好吗?

发布于 2024-10-03 03:42:23 字数 876 浏览 6 评论 0原文

我会在周六下午 6 点到周日凌晨 4 点之间显示一条消息。上次我不得不这样做时,它不起作用,因为我在将其更改为纽约时间时没有考虑到 UTC 时间为负数。

我的数学计算正确(在适当的时间显示)?我应该将 UTC 转换代码放入其自己的函数中吗?这是你见过的最糟糕的 js 吗?

-- jquery 被称为 --

$(document).ready(function() {
var dayTime = new Date();
var day = dayTime.getUTCDay();
var hour = dayTime.getUTCHours();
    //alert(day.toString()+" "+hour.toString());
    if (hour >= 5){
         hour = hour-5;          
    }
    else{
        hour = hour+19;
            if(day > 0){
                day--;
            }
            else{
                day = 6;    
            }
    }
    //alert(day.toString()+" "+hour.toString());
    if ((day == 6 && hour >= 18) || (day == 0 && hour < 4)){            
    }
    else{
        $('#warning').hide();  //Want this message to show if js is disabled as well
    }
});

I'm displaying a message between Saturday at 6pm and Sunday 4am. The last time I had to do this it didn't work because I didn't take into account UTC time going negative when changing it to NYC time.

I am doing the math right (displaying at the appropriate times)?Should I put the UTC conversion code into its own function? Is this the worst js you've ever seen?

-- jquery is called --

$(document).ready(function() {
var dayTime = new Date();
var day = dayTime.getUTCDay();
var hour = dayTime.getUTCHours();
    //alert(day.toString()+" "+hour.toString());
    if (hour >= 5){
         hour = hour-5;          
    }
    else{
        hour = hour+19;
            if(day > 0){
                day--;
            }
            else{
                day = 6;    
            }
    }
    //alert(day.toString()+" "+hour.toString());
    if ((day == 6 && hour >= 18) || (day == 0 && hour < 4)){            
    }
    else{
        $('#warning').hide();  //Want this message to show if js is disabled as well
    }
});

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

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

发布评论

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

评论(2

独留℉清风醉 2024-10-10 03:42:23

为什么你还需要 UTC 的东西?只需使用当地时间:

var day = dayTime.getDay();
var hour = dayTime.getHours();

您也可以稍微清理一下条件:

if (!(day == 6 && hour >= 18) && !(day == 0 && hour < 4)) {
    $('#warning').hide();
}

Why do you even need that UTC stuff? Just work with local time:

var day = dayTime.getDay();
var hour = dayTime.getHours();

And you can clean up that conditional a bit too:

if (!(day == 6 && hour >= 18) && !(day == 0 && hour < 4)) {
    $('#warning').hide();
}
夏了南城 2024-10-10 03:42:23

这应该可以得到服务器的时间:

var dayTime = new Date();
localOffset = dayTime.getTimezoneOffset() * 60000;
serverOffset = 5 * 60 * 60000;
dayTime = new Date(dayTime.getTime() + (localOffset - serverOffset));

在服务器偏移量中使用“5”;这是时间。它可能需要是-5;我不太确定。

而且,这将打破每个夏令时。您必须以某种方式检测到这一点并修改 serverOffset。

This should get you your server's time:

var dayTime = new Date();
localOffset = dayTime.getTimezoneOffset() * 60000;
serverOffset = 5 * 60 * 60000;
dayTime = new Date(dayTime.getTime() + (localOffset - serverOffset));

Play around with that "5" in the server offset; it's the hours. It may need to be a -5; I'm not really sure.

Also, that's going to break every daylight savings. You'll have to detect that somehow and modify serverOffset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文