2013-07-24 97 views
0

我正在使用鈦的應用程序,我需要在2周的範圍內獲取所有日期。獲取在2周範圍內的所有日期

例如,今天的日期是2013-24-07,我需要讓所有的日期,直到2013年7月8日這樣的:

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

等等......

我做一個test與代碼我發現here但我無法得到它的工作。

任何幫助是非常讚賞,

感謝

回答

3

嘗試這樣:

// 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; 
} 

,並使用它是這樣的:

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

在該方法的結束時,dates陣列將包含間隔的日期。

看一看:http://jsfiddle.net/5UCh8/1

2
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) 
+0

太棒了!感謝您的快速回答,在jsFiddle中進行測試,效果很好。 – Jef

3

我用Google搜索你的問題,並發現了這種代碼:

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

讓我知道你是否需要提供幫助的。 古德勒克

相關問題