我想用javascript写一个for循环,遍历日期格式是2015-03-21到2015-04-12之间的数据,如何写?

wnnnnnn6 2015-06-03 07:58:37
我的步骤是:
1、先求出从2015-03-21到2015-04-12之间的天的个数,比如是n;
2、用for循环遍历:
for(var i =0;i<n;i++){

show('2015-03-21'+i);//show是一个我自己写的javascript函数,该函数的参数是一个字符串类型的日期参数
//直接加i,我也知道不对,可是,我不会写。请问:这里该如何写?




总不能写成下面的形式吧:

show('2015-03-21');
show('2015-03-22');
show('2015-03-23');
show('2015-03-24');
……
……
……
show('2015-04-12');
...全文
577 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
scscms太阳光 2015-06-03
  • 打赏
  • 举报
回复
var start = new Date("2015-03-21".replace(/-/g,"/"));
var end = new Date("2015-04-12".replace(/-/g,"/"));
do{
  show(start.getFullYear()+"-"+(start.getMonth()+1)+"-"+start.getDate());
  start.setDate(start.getDate()+1);
}while(end >= start);

function show(ymd){
  console.log(ymd)
}
  • 打赏
  • 举报
回复
date.getDate()上面写错了,date.GetDay()是一个星期的第几天
  • 打赏
  • 举报
回复
var date = new Date(); 
console.info(new Date(date.getFullYear(),date.getMonth(),date.getDay()+1));
for循环加上去吧
wnnnnnn6 2015-06-03
  • 打赏
  • 举报
回复
引用 1 楼 slwsss 的回复:
setDate var a=new Date(2015,2,21); for(var i=0;i<15;i++)console.log(a.setDate(a.getDate()+1)&&a)
你的方法都不行
slwsss 2015-06-03
  • 打赏
  • 举报
回复
setDate var a=new Date(2015,2,21); for(var i=0;i<15;i++)console.log(a.setDate(a.getDate()+1)&&a)

87,955

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧