Skip to content

Instantly share code, notes, and snippets.

@dotcomboom
Last active July 28, 2018 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotcomboom/ee9e6f0d0bebe30267c563049c0cda64 to your computer and use it in GitHub Desktop.
Save dotcomboom/ee9e6f0d0bebe30267c563049c0cda64 to your computer and use it in GitHub Desktop.
Redirect to another URL based on the current date
/* configuration */
var redirectDays = [
['7/27/2018', 'https://dotcomboom.neocities.org'],
['7/28', 'https://coolandgood.neocities.org']
]; /* mm/dd/yyyy or mm/dd format */
var milliseconds = 2000;
/* sleep function from https://www.sitepoint.com/delay-sleep-pause-wait/ */
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
/* count occurrences of character from https://stackoverflow.com/a/881147 */
String.prototype.count=function(s1) {
return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length;
}
/* now, determine today's date */
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
var todaystr = mm + '/' + dd + '/' + yyyy;
console.log('Today is ' + todaystr);
/* add year to dates without it */
for (var i in redirectDays) {
if (redirectDays[i][0].count('/') < 2) {
redirectDays[i][0] = redirectDays[i][0] + '/' + yyyy
}
}
/* now we're ready to see if today has a redirect */
for (var i in redirectDays) {
console.log(redirectDays[i][0] + ', ' + redirectDays[i][1]);
if (redirectDays[i][0] == todaystr) {
console.log('Found a match, now redirecting')
sleep(milliseconds);
window.location.href = redirectDays[i][1];
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment