0

我想制作一些 Javascript 代码来查找截至当前日期的所有日期。

这是我到目前为止所拥有的:

var titleArray = [
"title1",
"title2",
];

var pictureArray = today.toString();
var thumbArray = today.toString();

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
}

if(mm<10) {
    mm='0'+mm
}

today = yyyy+'_'+mm+'_'+dd;

$.each(titleArray, function(i, val) {
    $(".dailySection").append('<a href="images/daily/'+pictureArray+'.jpg" title="'+val+'"><img src="images/thumbs/'+thumbArray+'.jpg"></a>');
});

如何在 Javascript 中创建日期数组?

4

1 回答 1

1

创建一个数组,然后使用push(). today您还应该记住,在实例化之前您无法引用,pictureArray因此thumbArray需要移动。此外,除非你打算做更多的事情而不是让他们每个人都持有相同的变量,否则我建议摆脱它们。

另外,我相信today在您调用它时应该已经是一个字符串toString()

var titleArray = [
    //a bunch of already-made and validated date strings
];

var myArray = [];

var today = new Date(); //it's a date!
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10){
    dd='0'+dd;
} 

if(mm<10){
    mm='0'+mm;
} 

today = yyyy+'_'+mm+'_'+dd; //now it's a string

/* 
Unnecessary? Just use titleArray[i] 
var pictureArray = today;
var thumbArray = today;
*/

titleArray.push(today);

$.each(titleArray, function(i, val){
    $(".dailySection").append('<a href="images/daily/'+val+'.jpg" title="'+val+'"><img src="images/thumbs/'+val+'.jpg"></a>');
});
于 2013-09-23T20:12:52.777 回答