0

I want to make some Javascript code to find all the days up to the current date.

Here is what I have so far:

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

How to create an array of Dates in Javascript?

1 Answer 1

1

Create an array, then use push(). You should also remember that you cannot reference today before it is instantiated, so pictureArray and thumbArray need to get moved. Besides, unless you plan on doing something more than have each of them hold onto the same variable, I suggest getting rid of them.

Also, I believe today should already be a string by the time you call toString() on it.

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>');
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.