displaying time at desired place

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ameen syed
    New Member
    • Nov 2007
    • 1

    displaying time at desired place

    hi iam ameen i want to display system time at desired place

    this is the code plz help me

    [HTML]<SCRIPT LANGUAGE="JAVAS CRIPT">
    <!--


    var d_names = new Array("Sunday", "Monday", "Tuesday",
    "Wednesday" , "Thursday", "Friday", "Saturday") ;

    var m_names = new Array("January" , "February", "March",
    "April", "May", "June", "July", "August", "September" ,
    "October", "November", "December") ;

    var d = new Date();
    var curr_day = d.getDay();
    var curr_date = d.getDate();
    var sup = "";
    if (curr_date == 1 || curr_date == 21 || curr_date ==31)
    {
    sup = "st";
    }
    else if (curr_date == 2 || curr_date == 22)
    {
    sup = "nd";
    }
    else if (curr_date == 3 || curr_date == 23)
    {
    sup = "rd";
    }
    else
    {
    sup = "th";
    }
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear() ;


    var tm=d_names[curr_day] + " " + curr_date + "<SUP>"
    + sup + "</SUP> " + m_names[curr_month] + " " + curr_year;
    document.write( d_names[curr_day] + " " + curr_date + "<SUP>"
    + sup + "</SUP> " + m_names[curr_month] + " " + curr_year);
    document.forms[0].Time.value=tm;
    function tt(){
    curr_date;
    }






    //-->
    </SCRIPT>
    </head>

    <body>
    <div align="center">

    <table width="187" height="101" border="1" align="center" cellpadding="4" cellspacing="5" bordercolor="#6 6FF00" bgcolor="#0000F F">
    <tr>
    <td class="style1"> TIME </td>
    <td><div align="center">
    <input name="Time" type="text" onClick=tm />
    </div></td>
    </tr>
    </table>
    </div>
    </body>
    </html>[/HTML]
    Last edited by gits; Nov 6 '07, 08:11 AM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Hi ameen, welcome to TSDN!

    Put the code from line 12 to 40 in a function, say getTime(). Then change the onclick to call this function.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      hi ...

      there are some more things to change i think ... you don't have a form tag, so you need another way of referencing the output element, give the textfield an id and refer to it with getElementById( ) ... i made your code working as an example ... have a look at it:

      [HTML]<html>
      <SCRIPT LANGUAGE="JAVAS CRIPT">
      var d_names = new Array("Sunday", "Monday", "Tuesday",
      "Wednesday" , "Thursday", "Friday", "Saturday") ;

      var m_names = new Array("January" , "February", "March",
      "April", "May", "June", "July", "August", "September" ,
      "October", "November", "December") ;

      var d = new Date();
      var curr_day = d.getDay();
      var curr_date = d.getDate();
      var sup = "";

      if (curr_date == 1 || curr_date == 21 || curr_date ==31)
      {
      sup = "st";
      }
      else if (curr_date == 2 || curr_date == 22)
      {
      sup = "nd";
      }
      else if (curr_date == 3 || curr_date == 23)
      {
      sup = "rd";
      }
      else
      {
      sup = "th";
      }

      var curr_month = d.getMonth();
      var curr_year = d.getFullYear() ;

      function set_time() {
      var tm=d_names[curr_day] + " " + curr_date + sup + " " + m_names[curr_month] + " " + curr_year;

      document.getEle mentById('time_ field').value=t m;
      }
      </SCRIPT>
      </head>

      <body onload="set_tim e();">
      <div align="center">

      <table width="187" height="101" border="1" align="center" cellpadding="4" cellspacing="5"

      bordercolor="#6 6FF00" bgcolor="#0000F F">
      <tr>
      <td class="style1"> TIME </td>
      <td><div align="center">
      <input name="Time" id="time_field " type="text"/>
      </div></td>
      </tr>
      </table>
      </div>
      </body>
      </html>
      [/HTML]
      kind regards

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by gits
        hi ...

        there are some more things to change i think ... you don't have a form tag, so you need another way of referencing the output element, give the textfield an id and refer to it with getElementById( )
        Yes, that's right. I didn't notice the missing form tag and, anyway, an ID is always the best way to get an element.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by acoder
          Put the code from line 12 to 40 in a function, say getTime(). Then change the onclick to call this function.
          Obs! That should have been line 41, not 40. Use the modified code provided by gits unless you want to update the time onclick, e.g. by clicking a button.

          Comment

          Working...