views:

512

answers:

4

I am wanting to have a series of dates (mainly Month, Day, Year) displayed within a vertical arrangement of table cells on a web page. The first date needs to be the current date minus one day, with the next date in the sequence be the current date, The remaining dates need to incrementally be one day in future out to 16 days.

Can someone provide help me figure out how to do this? I have looked at and understand a javscript to manipulate and display a single date (add or subtract) but am unable to get that date in a cell as well as figure out how to display the other multiple dates mentioned above in a html table.

Thanks in advance.

A: 

Did you try myDate.toString() or myDate.toDateString()?

What you need to do is have some variables holding a date... Like this

var myDate = new Date();

Put whatever date in it that you fancy, then do this.

myDate.toDateString()

You can create your table in a loop in javascript and fill it with dates.

Did this help?

Cyril Gupta
A: 

Option 1: You can use write the output into the document like:

<table>
<tr>
  <td><script type="text/javascript">document.write(mydate);</script></td>
  ...
</tr>

Option 2: generate the markup in javascript and then inject it into the DOM:

var markup = '<table>\
<tr>\
  <td>' + mydate + '</td>\
</tr>\
...
</table>';
document.getElementById('contentDiv').innerHTML = markup;

Where you have a div element in your page:

<div id="contentDiv"></div>
toby
A: 

Thanks so much for the help.

+1  A: 

Try this:

HTML

<table id="myTable"></table>

JavaScript

var table = document.getElementById('myTable')
var myDate = new Date();
myDate.setDate(myDate.getDate() - 1)
for(var i = 0; i < 16; i++)
{
    var row = document.createElement('TR');
    var cell = document.createElement('TD');  
    cell.innerText = myDate.getDate() + "/" + (myDate.getMonth() + 1) + "/"  + myDate.getYear();
    myDate.setDate(myDate.getDate() + 1)
    row.appendChild(cell);
    table.tBodies[0].appendChild(row);
}
Phaedrus