【问题标题】:javascript function return date array for all Sundays in a yearjavascript函数返回一年中所有星期日的日期数组
【发布时间】:2019-04-09 20:32:28
【问题描述】:

我正在用 javascript 编写一个函数,它将返回所有星期天的日期数组。 下面你可以看到我的代码:

function getDefaultOffDays(year){
var offdays=new Array();
i=0;
for(month=1;month<12;month++)
{
    tdays=new Date(year, month, 0).getDate();

    for(date=1;date<=tdays;date++)
    {
        smonth=(month<10)?"0"+month:month;
        sdate=(date<10)?"0"+date:date;
        dd=year+"-"+smonth+"-"+sdate;

        day=new Date();
        day.setDate(date);
        day.setMonth(month);
        day.setFullYear(year);

        if(day.getDay() == 0 )
             {              
               offdays[i++]=dd;

             }
    }
}

return offdays;
}

问题是返回的数组给出的是随机日期而不是星期天的唯一日期:( 我错过了什么?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    如果您检查结果,您会发现它实际上不是随机的。它返回 1 月的日期,即 2 月的星期日,依此类推。

    Date 对象的month 属性是从零开始的,而不是从一开始的。如果您更改此行,该函数将返回正确的日期:

    day.setMonth(month - 1);
    

    此外,循环仅从 1 运行到 11,您也需要包含 12 月:

    for (month=1; month <= 12; month++)
    

    另一种方法是找到第一个星期日,然后一次向前推进 7 天:

    function getDefaultOffDays2(year) {
      var date = new Date(year, 0, 1);
      while (date.getDay() != 0) {
        date.setDate(date.getDate() + 1);
      }
      var days = [];
      while (date.getFullYear() == year) {
        var m = date.getMonth() + 1;
        var d = date.getDate();
        days.push(
          year + '-' +
          (m < 10 ? '0' + m : m) + '-' +
          (d < 10 ? '0' + d : d)
        );
        date.setDate(date.getDate() + 7);
      }
      return days;
    }
    

    【讨论】:

      【解决方案2】:

      一个错误:

      for(month=1;month<12;month++)
      

      这只是 11 个月。

      如果你想要一整年,你需要:

      for(month=0;month<12;month++)
      

      因为一年有 12 个月。您甚至可以将其与 Guffa 的答案结合起来。

      【讨论】:

        【解决方案3】:

        月份是零,就像一天一样,所以如果月份是 0,那么它就是一月,所以改变你的代码如下,

        function getDefaultOffDays(year){ 
        var offdays=new Array();
        i=0;
        for(month=0;month<12;month++) { 
            tdays=new Date(year, month, 0).getDate(); 
            for(date=1;date<=tdays;date++)     {
                smonth=(month<10)?"0"+(month+1):(month+1);
                sdate=(date<10)?"0"+date:date;
                dd=year+"-"+smonth+"-"+sdate;
                var day=new Date(year,month,date);
                if(day.getDay() == 0 )              {
                    offdays[i++]=dd;
                }
              }
            }
            return offdays; 
          }
        

        【讨论】:

          【解决方案4】:

          这个函数有错误 错误代码:

          tdays=new Date(year, month, 0).getDate(); 
          

          替换为:

          tdays=new Date(year, month, 1).getDate(); 
          

          因为 0(day) 返回上个月

          【讨论】:

            猜你喜欢
            • 2015-12-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-04-08
            • 2018-02-28
            • 1970-01-01
            • 2012-10-01
            • 2013-02-27
            相关资源
            最近更新 更多