用js返回两个日期之间所有的日期组成的字符串,用逗号隔开
比如:返回2013-02-27和2013-03-01之间的所有日期
想要得到字符串:2013-02-27,2013-02-28,2013-03-01
var start = new Date('2013-02-27').getTime() , end = new Date('2013-03-01').getTime(); for(var i = start ; i <= end ;i+=24*60*60*1000){ alert(new Date(i).toLocaleDateString()); }
var start = new Date('2013-02-27').getTime() , end = new Date('2013-03-01').getTime(); var dates = [] , date , year , month , day; for(var i = start ; i <= end ;i+=24*60*60*1000){ date = new Date(i); year = date.getFullYear(); month = date.getMonth()+1; day = date.getDate(); dates.push(year+'-'+month+'-'+day); } alert(dates.join(','));
2013 , 01 , 27其实表示的是2013 , 02 , 27,因为月份减了1,所以变成2013 , 01 , 27了。
var start = new Date(2013 , 01 , 27).getTime() , end = new Date(2013 , 02 , 01).getTime(); var dates = [] , date , year , month , day; for(var i = start ; i <= end ;i+=24*60*60*1000){ date = new Date(i); year = date.getFullYear(); month = date.getMonth() + 1; day = date.getDate(); dates.push(year+'-'+month+'-'+day); } alert(dates.join(','));