我的网站目前.pastTV
在美国东部标准时间下午 12:00-12:30 期间显示 div。但是,该代码不是特定于时区的,它捕获任何访问站点的本地时间。
有没有办法让这个特定于 UTC 偏移量或时区,以便每个人都看到(或没有看到)相同的元素?
当访问者在东部时区时,我的代码按预期工作,否则它会读取本地用户时间。
更新:使用此代码代替相同的效果
var NYDate = new Date(new Date().toLocaleString("en-US", {timeZone: "America/New_York"}));
var NYHour = NYDate.getHours();
var NYMins = NYDate.getMinutes()
//12pm
if (NYHour >= 12 && NYHour <= 12 &&
NYMins > 0 && NYMins < 30) {
$('.pasttv, .life2').toggle();
}
var Now = new Date();
var CurrentDay = Now.getDay();
// opening time - 24 hours so 12:00pm is 12, 00
var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 12, 00);
// closing time - 24 hours so 12:30pm is 12, 30
var ClosingTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 12, 30);
var Open = (Now.getTime() > OpeningTime.getTime() && Now.getTime() < ClosingTime.getTime())
// days 0.sun 1.mon 2.tues 3.wed 4.thur 5.fri 6.sat
// CurrentDay !== 0 && the # is the day to eclude, so if I want to be closed on Sat6, Sun0, Wed3
// CurrentDay !== 6 && CurrentDay !== 0 && CurrentDay !== 3 && Open
if (CurrentDay !== 0 && CurrentDay !== 6 && Open) {
$('.pasttv').toggle();
}
.hours {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hours pasttv">
<font class="show-title">Past TVense</font><br> The first shows of the television medium
</div>