开发者

Appending multiple divs with date IDs

开发者 https://www.devze.com 2023-03-05 16:31 出处:网络
I\'m trying to create sort of a lifestream, in which I would display tweets and such. Anyway, I\'m at the beginning, an开发者_JAVA百科d I\'m stuck on something that must be quite easy but that I can\'

I'm trying to create sort of a lifestream, in which I would display tweets and such. Anyway, I'm at the beginning, an开发者_JAVA百科d I'm stuck on something that must be quite easy but that I can't figure out.

I'm trying to append to a container a series of divs (30 to be exact) in a way that each div has a class/ID with the date it concerns. I already have a date variable containing the current date in the format dd-mm-yyyy . I would like to append to my container something like :

<div class="entry" id="12-05-2011">
</div>
<div class="entry" id="11-05-2011">
</div>
<div class="entry" id="10-05-2011">
</div>
etc...

the first one being dated at the current date.

Do you have any ideas on how to do this ?

Thanks in advance !

Here is my date code :

var time = new Date();
var day = time.getDate();
var month = time.getMonth() + 1;
if(month < 10){
month = "0" + month;
}
var year = time.getFullYear();
var date = day + "-" + month + "-" + year;


Here is some JS you can do:

date_counter = {
    current_day: (new Date()).getDate(),
    current_month: (new Date()).getMonth(),
    current_year: (new Date()).getFullYear(),
    get_date: function(days_past){
        var time = new Date(this.current_year, 
                            this.current_month, 
                            (this.current_day)+days_past);
        var day = time.getDate();
        var month = time.getMonth() + 1;
        if (month < 10) {
            month = "0" + month;
        }
        var year = time.getFullYear();
        var date = day + "-" + month + "-" + year;
        return date
    }
}

for (var i = 0; i <= 30; i++)
{
    console.log(date_counter.get_date(i));
    var date = date_counter.get_date(i);
    $the_div = $('<div>', {
        id: 'date_' + date, //to have correct types of ids (see comments below)
        text: 'Content for ' + date
    });
    $the_div.appendTo(document.body);
}

And here is the fiddle example: http://jsfiddle.net/maniator/zrnnP/


Not sure why Neal's answer is so complex. Maybe I'm missing something, but the following seems to work for me...

var date = new Date()
date.setMonth(date.getMonth() - 1);

while(date <= new Date()) {
    $('#container').append(getDateDiv(date));
    date.setDate(date.getDate() + 1);
}

function getDateDiv(date) {
    return '<div id="' + getDateString(date) + '"></div>';
}

function getDateString(date) {
   // note that months are 0 indexed
   return date.getDate() + '-' 
       + (date.getMonth() + 1) 
         + '-' + date.getFullYear();
}

EDIT just seen you want descending order, so...

var date = new Date()
var counter = 0;
while(counter <= 30)
{
    $('#container').append(getDateDiv(date));
    date.setDate(date.getDate() - 1);
    counter++;
}

As an aside, I'd recommend using the date in yyyymmdd format instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消