code for Clock

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brigitte Behrmann
    New Member
    • Jun 2007
    • 25

    code for Clock

    Please can some-one help me figure out what is wrong with this code, I cannot get this to work...thanks

    Code:
    <html>
    <head>
    <title>Digital Clock</title>
    <script type="text/javascript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    function curTime() {
          var now = new date();
          var day = now.GetDay();
          var date = now.GetDate();
          var year = now.GetFullYear();
          var month = now.GetMonth();
          var hours = now.GetHours() + 1;
          var minutes = now.GetMinutes();
          var seconds = now.GetSeconds();
          var days = new Array();
          days[0] = "Sunday"; 
          days[1] = "Monday"; 
          days[2] = "Tuesday";
          days[3] = "Wednesday"; 
          days[4] = "Thursday"; 
          days[5] = "Friday";
          days[6] = "Saturday";
          var display = days[day] + " " + month + "/" + date + "/" + year + " "+ hours + ":" + minutes + ":" + seconds;
          document.forms[0].readout.value = display;
          var tick = setInterval("curTime()", 1000);
    }
    
    //STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </script>
    </head>
    <body>
    <form action="">
    <p><input type="text" size="60" name="readout" /></p>
    </form>
    </body>
    </html>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    you have to correct your instance of new Date() (UpperCase)! and the getters ... they start with lowerCase (i.e. getDay()). and you have to call your function - probably onload of your page ...

    kind regards ...

    Comment

    • Brigitte Behrmann
      New Member
      • Jun 2007
      • 25

      #3
      Originally posted by gits
      hi ...

      you have to correct your instance of new Date() (UpperCase)! and the getters ... they start with lowerCase (i.e. getDay()). and you have to call your function - probably onload of your page ...

      kind regards ...
      Thanx Gits. I know what you say is supposed to make sense but I have only been doing this for a week. I feel comfortable with the HTML and design part of the subject, it is just the javascript concepts and techniques I have not got the hang of yet... lots of studying ahead of me!

      I have made the following fixes in the code and tried the onLoad in the body, but this does not seem to work. Have I misunderstood you??
      Code:
      <html>
      <head>
      <title>Digital Clock</title>
      <script type="text/javascript">
      <!-- HIDE FROM INCOMPATIBLE BROWSERS
      function curTime() {
            var now = newDate();
            var day = now.getDay();
            var date = now.getDate();
            var year = now.getFullYear();
            var month = now.getMonth();
            var hours = now.getHours() + 1;
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();
            var days = new Array();
            days[0] = "Sunday"; 
            days[1] = "Monday"; 
            days[2] = "Tuesday";
            days[3] = "Wednesday"; 
            days[4] = "Thursday"; 
            days[5] = "Friday";
            days[6] = "Saturday";
            var display = days[day] + " " + month + "/" + date + "/" + year + " "+ hours + ":" + minutes + ":" + seconds;
            document.forms[0].readout.value = display;
            var tick = setInterval("curTime()", 1000);
      }
      
      //STOP HIDING FROM INCOMPATIBLE BROWSERS -->
      </script>
      </head>
      <body onLoad="curTime()">
      
      <form name="form" action="">
      <p><input type="text" size="60" name="readout" /></p>
      </form>
      </body>
      </html>

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        ... put a blank between new and Date() ...

        explaination: using new is supposed to create an instance of an javascript object and we want one of type date. so doing:

        [CODE=javascript]
        var date = new Date();
        [/CODE]

        creates an instance of the javascript date-object and the variable date is the reference to it. now you may use date.getDay() or any other methods the Date-object provides ...

        kind regards ...

        Comment

        • Brigitte Behrmann
          New Member
          • Jun 2007
          • 25

          #5
          Originally posted by gits
          ... put a blank between new and Date() ...
          Wow.... as simple as that and Voila it works. If I knew who you were I would send you the biggest box of chocolates.
          Your help is really appreciated. At least I will not have to submit blank assignments and I promise I will study really hard for the exams at the end of the year! Do you have any advice on websites with decent study material. The assigned text book - "JavaScript - Complete Ceoncepts & Techniques 2nd Edition" by Shelly Cashman, is okay but does not explain things very clearly!

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            ... hmmm ... would be nice ;)) ... your name sounds german (excuse me if i'm wrong) am i right? in that case the best way to search for javascript-issues would be to go to selfhtml:



            there you find a lot of js-related things - examples, explainations, etc. - it is the first adress for beginners i think ... don't prefer to use books ... try to search the web for tutorials and try everything for yourself ... when you are ready ... that means you know the basics and write those little scripts for yourself, then you may use books to read about the advanced concepts ... and the things how objects etc. work ... i think the best way is to try ... and then to read ... but it may be wrong for you ... its up to you ... which order you prefer, but selfhtml is a real good page for german learners ;)

            kind regards ...

            Comment

            • Brigitte Behrmann
              New Member
              • Jun 2007
              • 25

              #7
              Originally posted by gits
              ... hmmm ... would be nice ;)) ... your name sounds german (excuse me if i'm wrong) am i right? in that case the best way to search for javascript-issues would be to go to selfhtml:



              there you find a lot of js-related things - examples, explainations, etc. - it is the first adress for beginners i think ... don't prefer to use books ... try to search the web for tutorials and try everything for yourself ... when you are ready ... that means you know the basics and write those little scripts for yourself, then you may use books to read about the advanced concepts ... and the things how objects etc. work ... i think the best way is to try ... and then to read ... but it may be wrong for you ... its up to you ... which order you prefer, but selfhtml is a real good page for german learners ;)

              kind regards ...
              Thanks for the tip however I am South African - not german so it would need to be english language websites. The name is derived from french Brigitte and I married a Zimbabwean from German decent, hence the surname.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                ... ;) it sounds like a german name ... please excuse me ... ok? try to have a look at:

                W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


                may be it is of some help. since i'm a german and teached at the university i only know the real good german sites - until the students wanted them - or better prefered them ;) ... perhaps you might start a new thread to get more responses on that? but the above url shows you a lot things that are worth a look ;) ...

                kind regards ...

                Comment

                Working...