admin管理员组

文章数量:1403480

I am working on a application in Titanium where I need to get all the dates in a range of 2 weeks.

For example, today's date is 2013-24-07, I need to get all dates until 2013-07-08 like this:

var dates = [];

dates[0] = '2013-24-07';
dates[1] = '2013-25-07';
dates[2] = '2013-26-07';
dates[3] = '2013-27-07';
dates[4] = '2013-28-07';
dates[5] = '2013-29-07';
dates[6] = '2013-30-07';
dates[7] = '2013-31-07';
dates[8] = '2013-01-08';

And so on...

I made a test with the code I found here but I couldn't get it to work.

Any help is very much appreciated,

Thanks

I am working on a application in Titanium where I need to get all the dates in a range of 2 weeks.

For example, today's date is 2013-24-07, I need to get all dates until 2013-07-08 like this:

var dates = [];

dates[0] = '2013-24-07';
dates[1] = '2013-25-07';
dates[2] = '2013-26-07';
dates[3] = '2013-27-07';
dates[4] = '2013-28-07';
dates[5] = '2013-29-07';
dates[6] = '2013-30-07';
dates[7] = '2013-31-07';
dates[8] = '2013-01-08';

And so on...

I made a test with the code I found here but I couldn't get it to work.

Any help is very much appreciated,

Thanks

Share Improve this question edited May 23, 2017 at 12:14 CommunityBot 11 silver badge asked Jul 24, 2013 at 18:41 JefJef 8111 gold badge18 silver badges36 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Try something like this:

// create a extension for Dates like this
Date.prototype.addDays = function(days)
{
    var dat = new Date(this.valueOf());
    dat.setDate(dat.getDate() + days);
    return dat;
}

and use it something like:

// create the array
var dates = [];

// define the interval of your dates
// remember: new Date(year, month starting in 0, day);
var currentDate = new Date(); // now
var endDate = new Date(2013, 07, 07); // 2013/aug/07

// create a loop between the interval
while (currentDate <= endDate)
{
   // add on array
   dates.push(currentDate);

   // add one day
   currentDate = currentDate.addDays(1);
}

In the end of this method, the dates array will contain the dates of the interval.

Take a look here: http://jsfiddle/5UCh8/1

I googled your question, and found out this code:

var start = new Date("02/05/2013");
var end = new Date("02/10/2013");

while(start < end){
   alert(start);           

   var newDate = start.setDate(start.getDate() + 1);
   start = new Date(newDate);
}

Let me know if you need help with that. Goodluck

var start = Date.now();
var days = 14;
var dates = []
for(var i=0; i<days; i++)
    dates.push(new Date(start + (i * 1000 * 60 * 60 * 24)).toDateString());
alert(dates)

本文标签: javascriptGet all dates in a range of 2 weeksStack Overflow