Web Analytics Made Easy -
StatCounter Specify a different message for days and times - CodingForum

Announcement

Collapse
No announcement yet.

Specify a different message for days and times

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Specify a different message for days and times

    Hi all

    I'm a bewbie both here and to javascripting. I have a book and mission, but I'm not very good it it yet.

    I have a question, though please, if anyone can answer?

    I have been tasked with creating a small tool that will allow messages to be copied and pasted from a form. I have the form and have the basics of what I need but I am stuck with dates and times.

    Long story short, I am trying to figure out how to get a message to be displayed on a page using the document.write depending on the following criteria:

    If the day and time are monday to Friday 08:00 to 15:00 the message will state that the next update will be in 2 hours time from when this update was posted

    If the day and time are monday to thursday 15:00 to 18:00 the message will state that the next update will be at 09:00 the next working day (i.e. if it's 16:30 on a tuesday, the next update will be at 09:00 on wednesday etc)

    If the day and time are friday 15:00 to 18:00 the next message will state that the next update will be at 09:00 the next monday.

    Hopefully that makes sense but I have no idea where to start with this. Any heklp would be hugely appreciated.

    Thanks in advance

    Ian

  • #2
    Here you are:-

    Code:
    <div id = "messagebox"></div>
    
    <script type = "text/javascript">
    
    var now = new Date();
    var dy = now.getDay();
    var hh = now.getHours();
    var mess1 = "The next update will be in two hours time from when this update was posted.";
    var mess2 = "The next update will be at 0900 tomorrow."
    var mess3 = "The next update will be at 0900 this morning."
    var mess4 = "The next update will be at 0900 on Monday next"
    if (dy > 0 && dy <6 && hh <15 && hh >=8) {document.getElementById("messagebox").innerHTML = mess1}
    else if (dy > 0 && dy <5 && hh >=15) {document.getElementById("messagebox").innerHTML = mess2}
    else if (dy > 0 && dy <6 && hh <9) {document.getElementById("messagebox").innerHTML = mess3}
    else {document.getElementById("messagebox").innerHTML = mess4}
    
    </script>
    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    Presumably after 1500 (and not just 1500 - 1800) Mon - Thurs the next update is 0900 tomorrow.
    And between 0000 and 0900 Mon - Fri the next update is 0900 this morning (today).


    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.
    Last edited by Philip M; Apr 7, 2009, 12:45 PM.

    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.

    Comment


    • #3
      I'm still getting used to the correct etiquette here but thank you for your help. This is great and does exactly what I need.

      Cheers
      Ian

      Comment

      Working...
      X