1

I am extremely new to HTML and Javascript and have been winging it this far but Excel can only get me so far. Below is what I have laid out right now.

My idea is to have the I date always be todays date + the I Days ("=today() 14" in Excel)

My code is probably a mess but this is a work in progress and will eventually be part of a full table of about 8 rows after the header.

    <!DOCTYPE html>
    <html>
    <body>
    <table>
    <table border="1">
    <tr>
    <th>**Code**</th>
    <th>**Days**</th>
    <th>**Date**</th>
    </tr>
    <tr>
    <td id="I Code">I</td>
    <td id="I Days">14</td>    
    <td id="I Date">15/10/2017</td>
    </tr>
    </table>
    <input type="submit" value="Send">
    <input type="reset" value="Reset">
    </body>
    </html>
    </body>
    </html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Please see [*How do I ask a good question?*](http://stackoverflow.com/help/how-to-ask) – RobG Oct 16 '17 at 00:21

1 Answers1

0

Here is how you can do it with plain JavaScript:

<table>
    <table border="1">
    <tr>
    <th>**Code**</th>
    <th>**Days**</th>
    <th>**Date**</th>
    </tr>
    <tr>
    <td id="I Code">I</td>
    <td ><input type="number" value="0" onchange="update('row_1', this)"></td>    
    <td id="row_1">15/10/2017</td>
    </tr>
    </table>
    <script>
    window.update = function update(id, el) {
      var x = el.value;
      var toAdd = 1000 * 60 * 60 * 24 * x;
      var time = new Date(new Date().getTime() + toAdd);
      console.log(time)
      var formattedDate = (time.getMonth()+1) + '/' + time.getDate() + '/' + time.getFullYear();
      document.getElementById(id).innerHTML = formattedDate;
    }
    </script>

Here is a JsFiddle, so you can play around and see how it works.

Shimon Brandsdorfer
  • 1,673
  • 9
  • 23
  • Not all days are 24 hours long where daylight saving is observed, see [*Add +1 to current date*](https://stackoverflow.com/questions/9989382/add-1-to-current-date). Also, the OP does not seem to be using the peculiar US m/d/y format. – RobG Oct 15 '17 at 23:02
  • Thank you guys, this is going brilliant so far. i Don't use US date code as im in the UK. thank you for the quick input, however its making the table cells with the number of days in it "freetext" i need this to be preset for 14. and the Date cell to always been todays date plus the number of days in the days cell (14 in this case) – Mark Fitzmaurice Oct 17 '17 at 16:49
  • @MarkFitzmaurice If my answer was helpful, Please accept my answer – Shimon Brandsdorfer Oct 17 '17 at 17:36