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 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date & Time JavaScript Help

    I cannot get my JavaScript code to work. Does anyone know what I am doing wrong with the following code:
    <html>
    <head>
    <title>Welcome</title>
    </head>
    <body>
    <h1>Welcome to my Web Page</h1>
    <script type="text/javascript">
    /* <![CDATA[ */
    var dateObject = new Date();
    var greeting = "";
    var curTime = "";
    var minuteValue = dateObject.getMinutes();
    var hourValue = dateObject.getHours();
    if (minuteValue < 10) minuteValue = "0" + minuteValue;
    if(hourValue < 12) { greeting = "<p>Good morning! " curTime = hourValue + ":" + minuteValue + " AM"; }
    else if (hourValue == 12) { greeting = "<p>Good afternoon! "; + minute Value + " PM"; }
    else if (hourValue == 17) { greeting = "<p>Good afternoon!" cutTime = (hourValue-12) + ":" + minuteValue + " PM" }
    else { greeting = "<p>Good evening! " curTime = (hourValue-12) + ":" + minuteValue + " PM" }
    var dayArray = newArray("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var monthArray = newArray("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var day = dateObject.getDay();
    var month = dateObject.getMonth();
    document.write("<p>"+ greeting + "It is " + curTime + " on " + dayArray[day] + ", " + monthArray[month] + " " + dateObject.getDate() + ", " + dateObject.getFullYear() + ".</p>");
    /* ]]> */
    </script>
    </body>
    </html>

    ~xhtml205

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    A good many errors - missing semi-colons, cutTime, errors with == and <=

    Code:
    <html>
    <head>
    <title>Welcome</title>
    </head>
    <body>
    <h1>Welcome to my Web Page</h1>
    <script type="text/javascript">
    /* <![CDATA[ */
    var dateObject = new Date();
    var greeting = "";
    var curTime = "";
    var minuteValue = dateObject.getMinutes();
    var hourValue = dateObject.getHours() ;
    if (minuteValue < 10) minuteValue = "0" + minuteValue;
    if(hourValue < 12) { greeting = "<p>Good morning! "; curTime = hourValue + ":" + minuteValue + " AM"}; 
    else if (hourValue >= 12 && hourValue <=17)  { greeting = "<p>Good afternoon! "; curTime = (hourValue - 12) + ":" + minuteValue + " PM"}
    else { greeting = "<p>Good evening! "; curTime = (hourValue-12) + ":" + minuteValue + " PM" }
    
    var dayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var monthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var day = dateObject.getDay();
    var month = dateObject.getMonth();
    document.write("<p>"+ greeting + "It is " + curTime + " on " + dayArray[day] + ", " + monthArray[month] + " " + dateObject.getDate() + ", " + dateObject.getFullYear() + ".</p>");
    /* ]]> */
    </script>
    </body>
    </html>
    Last edited by Philip M; 04-30-2008 at 07:36 PM.

  • #3
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Phillip M,

    I made the changes of cutTime and added some semi-colons, however, I am still not getting it to work. Here is what my code looks like now:
    <html>
    <head>
    <title>Welcome</title>
    </head>
    <body>
    <h1>Welcome to my Web Page</h1>
    <script type="text/javascript">
    /* <![CDATA[ */
    var dateObject = new Date();
    var greeting = "";
    var curTime = "";
    var minuteValue = dateObject.getMinutes();
    var hourValue = dateObject.getHours();
    if (minuteValue < 10) minuteValue = "0" + minuteValue;
    if(hourValue < 12) { greeting = "<p>Good morning! " curTime = hourValue + ":" + minuteValue + " AM"; }
    else if (hourValue == 12) { greeting = "<p>Good afternoon! "; + minute Value + " PM"; }
    else if (hourValue == 17) { greeting = "<p>Good afternoon!" curTime = (hourValue-12) + ":" + minuteValue + " PM"; }
    else { greeting = "<p>Good evening! " curTime = (hourValue-12) + ":" + minuteValue + " PM"; }
    var dayArray = newArray("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var monthArray = newArray("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var day = dateObject.getDay();
    var month = dateObject.getMonth();
    document.write("<p>"+ greeting + "It is " + curTime + " on " + dayArray[day] + ", " + monthArray[month] + " " + dateObject.getDate() + ", " + dateObject.getFullYear() + ".</p>");
    /* ]]> */
    </script>
    </body>
    </html>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    See above. I corrected a couple of typos. It works fine now.

    Check out what is wrong here:

    else if (hourValue == 17) { greeting = "<p>Good afternoon!" ; curTime = (hourValue-12) + ":" + minuteValue + " PM"; }
    else { greeting = "<p>Good evening! " ; curTime = (hourValue-12) + ":" + minuteValue + " PM"; }
    Last edited by Philip M; 04-30-2008 at 07:40 PM.

  • #5
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, I see. You have made the changes for me. Thank you for helping me! Now I shall go and study the code.

    Again thanks.

    ~xhtml205

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    In fact it would be better/simpler to arrange your tests in reverse time order:-

    if (hourValue > 16) { ....... Evening
    else if (hourValue > 11) { ....... Afternoon
    else { ...... Morning

  • #7
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question small problem with this code.

    One Two Three Four Five
    Last edited by whyght mic; 06-30-2011 at 06:22 AM.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Change the code as follows:-

    Code:
    if (hourValue < 12) { greeting = "<p>Good morning! "; curTime = hourValue + ":" + minuteValue + " AM"}; 
    if (hourValue == 12)  { greeting = "<p>Good afternoon! "; curTime = hourValue + ":" + minuteValue + " PM"}
    if (hourValue > 12 && hourValue <=17)  { greeting = "<p>Good afternoon! "; curTime = (hourValue - 12) + ":" + minuteValue + " PM"}
    if (hourValue >17) { greeting = "<p>Good evening! "; curTime = (hourValue-12) + ":" + minuteValue + " PM" }
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 06-28-2011 at 05:19 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.

  • Users who have thanked Philip M for this post:

    whyght mic (06-30-2011)

  • #9
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Still not working

    One Two Three Four Five
    Last edited by whyght mic; 06-30-2011 at 06:22 AM.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Sorry, it works for me. It is not possible for the script to display AM when the hourValue is 12.

    Try adding the temporary line hourValue = 12 to set the hour to 12 for testing purposes regardless of the actual time. Then what I get is

    Good afternoon! It is 12:11 PM on Tuesday, June 28, 2011.

    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:

    whyght mic (06-30-2011)

  • #11
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Unhappy

    Awesome
    Last edited by whyght mic; 06-29-2011 at 10:30 AM.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    The fault, dear Brutus, lies not in the stars but ........ The hour after midnight is 0, not 12. In Post#7 you asked for a correction for 12:00PM, not 12:00 AM. And I have to say that you ought to be able to make that simple correction yourself following the code I showed you.

    Simply add another line:

    if (hourValue >17) { greeting = "<p>Good evening! ";
    curTime = (hourValue-12) + ":" + minuteValue + " PM" }
    if (hourValue == 0 ) { greeting = "<p>Good morning! ";
    curTime = 12 + ":" + minuteValue + " AM"};


    This is not a very good script (I did not write it - I simply corrected the original OP's errors) and I expect you realise that it is not a clock. It will only display the time that the user entered the site, and remain at that time throughout.

    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:

    whyght mic (06-29-2011)

  • #13
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Thumbs up

    One Two Three Four Five
    Last edited by whyght mic; 06-30-2011 at 06:21 AM.


  •  

    Posting Permissions

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