-2

In a JavaScript program, I have to get date in milliseconds or as a string in a format recognized by the parse method from user as input in textbox and then by clicking on a button convert it in toString and then clicking on second button getDate.

I m confused how to correct or complete this code!

my incomplete code is:

<html>
  <head>
    <title>Demonstration of Date object - Javascript</title>
  </head>
  <body topmargin="100px" leftmargin="130px">
    <p>
      <font size="4" color="navy" face="candara">Demonstration of using the Date object in JavaScript. Enter a Date as a number in milliseconds, or as a string in format recognized by the parse method.</font>
    </p>
    <form name="form1">
      <table>
        <tr>
          <td>Enter a Date:</td>
          <td><input type="text" name="txt1"></td>
        </tr>
      </table>
      <br>
      <table>
        <tr>
          <td>
            <input type="button" value="toString" onclick="tostring()">
            <p id="demo1">
          </td>
        </tr>
        <tr>
          <td>
            <input type="button" value="getDate" onclick="getdate()">
            <p id="demo2">
          </td>
        </tr>
        <tr>
          <td>
            <input type="button" value="getDay" onclick="getday()">
            <p id="demo3">
          </td>
        </tr>
        <tr>
          <td>
            <input type="button" value="toDateString" onclick="datestring()">
            <p id="demo4">
          </td>
        </tr>
        <tr>
          <td>
            <input type="button" value="getFullYear" onclick="getfullyear()">
            <p id="demo5">
          </td>
        </tr>
        <tr>
          <td>
            <input type="button" value="getMonth" onclick="getmonth()">
            <p id="demo6">
          </td>
        </tr>
        <tr>
          <td>
            <input type="button" value="getTime" onclick="gettime()">
            <p id="demo7">
          </td>
        </tr>
      </table>
    </form>
    <script type="text/javascript" language="javascript">
      function tostring() 
      {
      var d = new Date(document.form1.txt1.value);
      var n = d.toString();
      document.getElementById("demo1").innerHTML = n;
      }
    </script>
  </body>
</html>
DNA
  • 42,007
  • 12
  • 107
  • 146
  • You haven't closed your ` – DNA May 10 '15 at 10:35
  • I have added the `` and indented your code - please try to format your code in future when asking questions as it makes it so much easier for us to read and spot problems. – DNA May 10 '15 at 10:37

1 Answers1

0

The shortest possible answer would be: this can be optimized in many ways but it's not far from working. First of all your tostring() function should be called. If your field is a text input you should make sure it's called on the oninput event:

<input type="text" id="yourTextId" oninput="tostring()" />

If you don't want it reacting on every change you should just call it on the onclick event of a button.

You could improve some things though, e.g:

  • tostring() sounds like a function which is returning a string, but it's not, you are just filling a field (it's more like a method). fillTime() would maybe be better.
  • try to get references to the elements in a consistent way: document.form1.txt1.value seems strange to me because you use document.getElementById() just below. Personally i would use document.getElementById("txt1").value to get the number.
  • try to make us understand where the problem is better. Are you running this and getting an error? You can see errors in most browsers if you press F12 and read the developer console. Also, maybe you can format the code in your future posts.
Sandman
  • 2,577
  • 2
  • 21
  • 32