Need help with 3 daily messages rotating indefinitely
I'm sure the answer to this question will seem silly and basic in hindsight, but I just can't get my brain around it. I'd be grateful if someone could help or point me to a script that will accomplish what I'm trying to do.
I trying to come up with a script that will display one of 3 daily messages. Message 1 must display on a given date (for instance Jan. 1, 2012), and then messages 2 and 3 need to display on subsequent days before the cycle starts to repeat (in this case, on January 4, 2012) and then continues to repeat indefinitely into the future.
<span id = "mymessage"</span>
<script type = "text/javascript">
var today = new Date().getTime();
today = Math.floor(today / 86400000) // milliseconds in a day so today = number of days since 01/01/1970
var message1 = "This is message one";
var message2 = "This is message two";
var message3 = "This is message three";
if (today%3 == 0) {document.getElementById("mymessage").innerHTML = message3}
if (today%3 == 1) {document.getElementById("mymessage").innerHTML = message1} // shows on 1st January 2012
if (today%3 == 2) {document.getElementById("mymessage").innerHTML = message2}
</script>
It was exactly the same score as last season, except is was 0-0 instead of 1-1. - Commentator Sky Sports
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Last edited by Philip M; 10-24-2012 at 05:36 PM..
Reason: Typo
Thanks, Philip M. For some reason, the messages switch at 8 PM instead of at midnight. And actually what I'm looking for (and should have mentioned in the original post) is to have the messages switch at 8 AM. I've played around with the script but can't find anything to make things work correctly. Any thoughts?
The change of date occurs at midnight. I don't understand why you say it changes at 8.00pm unless the clock in your computer is wrong.
To change the time when the display changes to the following day at 0800
Code:
var today = new Date().getTime();
today = today - 28800000; // milliseconds in 8 hours to retard change to 0800
today = Math.floor(today / 86400000) // milliseconds in a day
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
PC time is correct and set via sync with Internet time server. I ended up having to change to
today = today - 43200000
in order to get the message to change at 8 AM, but it works. So weird. I'll check how things appear using some other computers and other browsers but for now things seem OK.
Are you in a different time zone to GMT? getTime() gets the milliseconds since 01/01/1970 UTC.
It helps when asking questions relating to time to indicate your location in your profile.
I am in the USA and am on Eastern Time (New York). I will update my profile and I appreciate the suggestion. Is there going to be a problem with the script when going on or off Daylight Saving Time?
One other question since you've been so helpful -- can the color of each of the various rotating messages be different?
I am in the USA and am on Eastern Time (New York). I will update my profile and I appreciate the suggestion. Is there going to be a problem with the script when going on or off Daylight Saving Time?
One other question since you've been so helpful -- can the color of each of the various rotating messages be different?
You will need to take account of DST in your timezone.
if (today's date is between the start of DST and end of DST) {add one hour}
Of course the color of the messages can be different - use css to style them.
Code:
if (today%3 == 0) {document.getElementById("mymessage").innerHTML = message3;
document.getElementById("mymessage").style.color = "blue"}
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.