I am trying to find a way to redirect a page to another page at exactly 7:58 EST Every day then redirect back at 9 PM EST every day any ideas or advice on this?
-
lots of googling with no luck haha all they talk about is like redirecting to a time of seconds not really a specific time – Jan 31 '19 at 00:15
-
What is "EST"? It might be one of 3 timezones. Work out the time you want to redirect in UTC and use that. – RobG Jan 31 '19 at 00:21
-
EST would be like New York time Or Toronto TIme Est – Jan 31 '19 at 00:25
-
12:58 AM UTC would be 7:58 pm EST – Jan 31 '19 at 00:26
-
There is only one way to accomplish this with a browser window open at the magic hour. Check the time every sec/15 secs/30 secs/45 secs etc. when the time hits, change the `document.location`. – Randy Casburn Jan 31 '19 at 00:26
2 Answers
I created something for you to trigger your function every x seconds and redirect to wherever you want
window.setInterval(function(){ //This will trigger every X miliseconds
var date = new Date(); //Creates a date that is set to the actual date and time
var hours = date.getHours(); //Get that date hours (from 0 to 23)
var minutes = date.getMinutes(); //Get that date minutes (from 0 to 59)
if (hours == 7 && minutes == 58 ){
//redirect to where you want
} else if( hour = 21 && minutes == 00){
//redirect to where you want
}
}, 1000); //In that case 1000 miliseconds equals to 1 second
Now, talking about UTC and setting it. You can set it when you create your date, there is a topic talking only about that: Create a Date with a set timezone without using a string representation AND How to initialize a JavaScript Date to a particular time zone
Sometimes I would recomend using something like moment.js. I hope my answer helps you someway

- 99
- 8
-
-
@JamesMOON If it is your UTC you can just create the date normally and then compare hours to 23 and minutes to 58 inside the if statement. – Arthur Paiva Jan 31 '19 at 10:06
Redirecting to another page is quite straight forward, however redirecting back to the originating page isn't as easy.
To achieve this effect, you might consider wrapping the pages you want to navigate to and from, in an <iframe>
element, where the page containing the <iframe>
controls the navigation at those key times.
You could also consider setting up up an interval to check for and run this navigation logic (if needed) every one minute using setInterval()
. To simplify the manipulation and querying of times (in relation to your timezone), you might find momentjs helpful.
These ideas are combined in the snippet below:
/*
Run this logic once per minute
*/
const oncePerMinute = () => {
/*
Get iframe and current iframe src
*/
const iframe = document.querySelector('iframe');
const iframeSrc = iframe.getAttribute('src');
/*
The url's to display for either time slot
*/
const dayUrl = 'daytimeurl'
const nightUrl = 'nighttimeurl'
/*
Adjust time to EST (-5hr)
*/
const momentEST = moment().utc().utcOffset('-0500');
/*
If between 7:58am and 9:00pm update iframe to show day url
*/
if(momentEST.isBetween(moment('7:58am', 'h:mma'), moment('9:00pm', 'h:mma'))) {
if(iframeSrc !== dayUrl) {
iframe.setAttribute('src', dayUrl);
}
}
/*
Othwise, update iframe to show night url
*/
else {
if(iframeSrc !== nightUrl) {
iframe.setAttribute('src', nightUrl);
}
}
}
/*
Will set interval to run oncePerMinute every minute
*/
setInterval(oncePerMinute, 1000 * 60);
oncePerMinute();
iframe {
position:fixed;
top:0;
left:0;
width:100%;
height:100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<iframe></iframe>

- 29,664
- 5
- 45
- 65
-
i just need to find a code that makes page a redirect at a specfic UTC time and then ill use the same code on page B to Redirect back at that UTC time – Jan 31 '19 at 04:15
-
-
Hi, just to confirm - all you need is a script the directs to another page at the specific time? You don't need that page to redirect the browser back to the starting page? – Dacre Denny Jan 31 '19 at 04:39
-
i didnt think so but i like your idea you have above can you make a Jfiddle for it? so i can see better how to set the code up? – Jan 31 '19 at 05:22
-
Sure - can you give me the url's that you want the script to redirect to at the given times? – Dacre Denny Jan 31 '19 at 05:29
-
i havent made them yet for the sake of the code just use http://page1 and http://page2 or random urls and ill know to replace those – Jan 31 '19 at 05:32
-
it seems when you plug in a url like i tried http://nodq.com/ as a test its just a blank white page no matter what url – Jan 31 '19 at 05:39
-
-
Yes, if you're trying to redirect the `iframe` to pages on another server, you'll usually find it will fail due to the origin of the request differing from that of the other server (my understanding was you were displaying pages in the `iframe` on your own server - is that correct?) – Dacre Denny Feb 01 '19 at 01:18