Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Location
    USA East Coast
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

    MANY thanks in advance.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Here you are:-
    Code:
    <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
    Last edited by Philip M; 10-24-2012 at 05:36 PM. Reason: Typo

    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.

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Location
    USA East Coast
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    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.

  • Users who have thanked Philip M for this post:

    Firedog (10-25-2012)

  • #5
    New to the CF scene
    Join Date
    Oct 2012
    Location
    USA East Coast
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

    Thank you again.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by Firedog View Post
    So weird.
    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.

    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.

  • #7
    New to the CF scene
    Join Date
    Oct 2012
    Location
    USA East Coast
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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?

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by Firedog View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •