简体   繁体   English

获取2周内的所有日期

[英]Get all dates in a range of 2 weeks

I am working on a application in Titanium where I need to get all the dates in a range of 2 weeks. 我正在Titanium中开发一个应用程序,我需要在2周内获取所有日期。

For example, today's date is 2013-24-07, I need to get all dates until 2013-07-08 like this: 例如,今天的日期是2013-24-07,我需要像这样获取所有日期直到2013-07-08:

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 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 祝好运

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. 在此方法的最后, dates数组将包含间隔的日期。

Take a look here: http://jsfiddle.net/5UCh8/1 在这里看看: http : //jsfiddle.net/5UCh8/1

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM