2016-06-22 17 views
-4

我的JavaScript數組就像那樣。 var datearray = ["2010-01-10","2010-01-20","2014-01-35","2014-10-22","2014-03-02","2010-02-11","2010-03-18","2010-09-09","2014-11-12","2014-02-23","2014-03-02","2014-03-02","2014-04-22","2014-05-09","2014-02-23","","2010-02-19","2010-03-01","2010-02-27","2010-02-25"];如何獲取使用javascript數組的選定的兩個日期之間的新陣列

但我的搜索日期是startDate = 2010-01-01;endDate = 2010-03-29;我想要在startDate和endDate之間獲取新的日期數組;我是如何創建它的。

+0

至少表現出一定的精力和發佈您的代碼 –

+3

'陣列#filter' .. – Rayon

+0

我不知道這個問題的背景下,但如果你打算做多隻對數組的這個操作,您應該將該數組映射到Date對象(或表示1970年1月1日以來的時間的整數)並將其排序。 – 4castle

回答

0

var dateArray = ["2010-01-10","2010-01-20","2014-01-35","2014-10-22","2014-03-02","2010-02-11","2010-03-18","2010-09-09","2014-11-12","2014-02-23","2014-03-02","2014-03-02","2014-04-22","2014-05-09","2014-02-23","","2010-02-19","2010-03-01","2010-02-27","2010-02-25"], 
 
    startDate = Date.parse("2010-01-01"), 
 
    endDate = Date.parse("2010-03-29"); 
 

 
var filteredDateArray = dateArray.filter(function(s) { 
 
    s = Date.parse(s); 
 
    return s > startDate && s < endDate; 
 
}); 
 

 
console.log(filteredDateArray);

相關問題