最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Inserting Dates into HTML Table Cells - Stack Overflow

programmeradmin0浏览0评论

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 Javascript 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.

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 Javascript 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.

Share Improve this question edited Jan 20, 2016 at 9:47 Termininja 7,05612 gold badges50 silver badges50 bronze badges asked Jan 30, 2009 at 20:04 MadDogMadDog
Add a ment  | 

3 Answers 3

Reset to default 1

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);
}

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?

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>
发布评论

评论列表(0)

  1. 暂无评论