【问题标题】:how can i get dates on saturday and sunday between two dates in javascript如何在javascript中的两个日期之间获取星期六和星期日的日期
【发布时间】:2015-04-09 17:38:56
【问题描述】:

我对像 javascript 这样的浏览器端脚本非常薄弱,因为我是一名 php 开发人员。我需要得到周六和周日的日期。我找到了很多计算计数的答案,但还没有找到在周六和周日获取日期的方法。我尝试了这些:

Date.prototype.endOfWeek = function(){
  return new Date( 
      this.getFullYear(), 
      this.getMonth(), 
      this.getDate() + 6 - this.getDay() 
  );
};

var now = new Date();

// returns next saturday; and returns saturday if it is saturday today.
alert(now.endOfWeek() ); 

它只在下一个星期六返回给我。请帮我得到这个

我也试过这个,但它返回给我计数

function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
        var iWeeks, iDateDiff, iAdjust = 0;
        if (dDate2 < dDate1) return -1; // error code if dates transposed
        var iWeekday1 = dDate1.getDay(); // day of week
        var iWeekday2 = dDate2.getDay();
        iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
        iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
        if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
        iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
        iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

        // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
        iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)

        if (iWeekday1 <= iWeekday2) {
          iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
                    alert(iDateDiff);
        } else {
          iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
        }

        iDateDiff -= iAdjust // take into account both days on weekend

        return (iDateDiff + 1); // add 1 because dates are inclusive
    }


alert(calcBusinessDays(new Date("August 11, 2010 11:13:00"),new Date("August 16, 2010 11:13:00")));

【问题讨论】:

  • 第一个函数返回下一个星期六的日期对象。是否要返回包含星期六和星期日的两个 Date 对象的数组?
  • 您想返回两个日期之间的所有个星期六和星期天,就在下一个周末,还是什么?

标签: javascript date datetime


【解决方案1】:

如何在两个日期之间获取星期六和星期日的日期 javascript

这样的事情会返回两天之间的所有周六和周日

function calcBusinessDays(dDate1, dDate2) {
    if (dDate1 > dDate2) return false;
    var date  = dDate1;
    var dates = [];

    while (date < dDate2) {
        if (date.getDay() === 0 || date.getDay() === 6) dates.push(new Date(date));
        date.setDate( date.getDate() + 1 );
    }

    return dates;
}

var d1 = new Date(2015, 3, 3);
var d2 = new Date(2015, 5, 3);

function calcBusinessDays(dDate1, dDate2) {
    if (dDate1 > dDate2) return false;
    var date  = dDate1;
    var dates = [];

    while (date < dDate2) {
        if (date.getDay() === 0 || date.getDay() === 6) dates.push(new Date(date));
        date.setDate( date.getDate() + 1 );
    }
    
    return dates;
}

document.body.innerHTML = '<pre>' + JSON.stringify(calcBusinessDays(d1,d2), null, 4) + '</pre>';

【讨论】:

  • 显示“2015-04-03T18:30:00.000Z”、“2015-04-04T18:30:00.000Z”,星期六是星期六,但实际上是星期六
  • 确实在这里,当我在小提琴中尝试它时,它会显示 4 月 4 日和 5 日,所以它确实有效,getDay 不可能是 0 或 6一个星期五,所以这里的 sn-p 发生了一些奇怪的事情?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 2017-05-02
  • 2019-08-02
  • 1970-01-01
相关资源
最近更新 更多