Javascript,如何创建日期数组?

时间:2013-09-23 19:52:55

标签: javascript jquery html

我想制作一些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中创建日期数组?

1 个答案:

答案 0 :(得分:1)

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

此外,我认为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>');
});