如何获取一周中存在于某个日期范围(Javascript)中的天数列表



您好,我需要定义某个日期范围内的现有日期。我不会从开始到结束日期进行循环并使用像这里一样 http://www.w3schools.com/jsref/jsref_getday.asp是否可以计算一些方法?

function getDays(earlierDate, laterDate) {
  var dayNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
  var elapsedDays = (laterDate - earlierDate) / 1000 / 60 / 60 / 24;
  if (elapsedDays < 7) {
    var dayArray = [];
    for (i = 0; i <= elapsedDays; i++) {
        dayArray.push(dayNames[(earlierDate.getDay()+i) % 7]);
    }
    return dayArray;
  }
  return dayNames;
}

并在 js 控制台中进行测试:

> getDays(new Date("03-01-2012"), new Date("03-01-2012"));
["Thursday"]
> getDays(new Date("03-01-2012"), new Date("03-05-2012"));
["Thursday", "Friday", "Saturday", "Sunday", "Monday"]
> getDays(new Date("03-01-2012"), new Date("03-05-2013"));
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
 var sevenDaysInMS = 604800000;
    if (new Date(dTo - dFrom).getTime() >= sevenDaysInMS) {
        $('.day_hid').attr(attrUnaviableDay, "true");
    } else {
        var dt = dFrom;
        do {
            var day = dt.getDay();
            ActivateDaySelector(day);
            dt.setDate(dt.getDate() + 1);
        } while (dt <= dTo);
    }

我觉得没关系。你觉得怎么样?

相关内容

  • 没有找到相关文章

最新更新