1

My website currently displays the .pastTV div during the hours of 12:00pm-12:30pm EST. However, the code is not time-zone specific, capturing the local time of anyone visiting site.

Is there a way to make this specific to a UTC offset or timezone so that everyone is seeing (or not seeing) the same element?

My code works as expected when a visitor is in the Eastern timezone, otherwise it reads the local user time.

Update: Used this code instead for the same effect

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>

0

Have you tried using this? Date.prototype.getTimezoneOffset()

More info below:

"The time-zone offset is the difference, in minutes, from local time to UTC. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, for time zone UTC+10:00 (Australian Eastern Standard Time, Vladivostok Time, Chamorro Standard Time), -600 will be returned."

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.