0

Hy guys, i have write this javascript code for calculate date for any day of week progressively. But i ahve error when the code must be calculate the end of month: example after 31/07/2'15 the code generate 32/07/2015 instead 01/08/2015. This is my code:

var application = this;
        var currentDate = new Date();
        var stringDate1 = currentDate.getUTCDate() + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate2 = (currentDate.getUTCDate()+1) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate3 = (currentDate.getUTCDate() + 2) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate4 = (currentDate.getUTCDate() + 3) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate5 = (currentDate.getUTCDate() + 4) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate6 = (currentDate.getUTCDate() + 5) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate7 = (currentDate.getUTCDate() + 6) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();

where my code is wrong? Any help please? BR

2
  • Are you trying to get the dates for today and the next 6 days? Either way, the way you are trying to achieve this is far too complicated.
    – Cos
    Jul 27, 2015 at 13:00
  • Yes, my code calculate the date for today and for the 6 next days.
    – dev_
    Jul 27, 2015 at 13:07

2 Answers 2

1

Just add the hours*minutes*seconds*milliseconds to today's date, and you get tomorrow's date, and so on.

var tomorrowsDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
1

To calculate the date for one day ahead you can use Date.prototype.setDate()

So to calculate all dates from today and week ahead, you can do something like this:

var currentDate = new Date();
var stringDate = [];

for (var i = 0; i < 7; ++i) {
    currentDate.setDate(currentDate.getDate() + 1);
    stringDate[i] = currentDate.getUTCDate() + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
}

console.log(stringDate);

// 0: "28/7/2015"
// 1: "29/7/2015"
// 2: "30/7/2015"
// 3: "31/7/2015"
// 4: "1/8/2015"
// 5: "2/8/2015"
// 6: "3/8/2015"
3
  • Ok thanks but i need for each day a variable string, so i must create 6 variable with the array. example: stringDate1 = stringDate(0), stringDate2 = stringDate(1) ect etc
    – dev_
    Jul 27, 2015 at 13:30
  • @dev_ Just access it like stringDate[0] for today, stringDate[1] for tomorrow and so on Jul 27, 2015 at 13:32
  • Yes, i jìhave associated the array to singles variable. Thanks for your help
    – dev_
    Jul 27, 2015 at 14:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.