我需要最快的方法得到一周的第一天。例如:今天是11月11日,是星期四;我想要这周的第一天,也就是11月8日,一个星期一。我需要MongoDB映射函数的最快方法,有什么想法吗?


当前回答

使用Date对象的getDay方法,您可以知道一周中的天数(0=星期日,1=星期一,等等)。

然后你可以用这个天数加1,例如:

function getMonday(d) {
  d = new Date(d);
  var day = d.getDay(),
      diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
  return new Date(d.setDate(diff));
}

getMonday(new Date()); // Mon Nov 08 2010

其他回答

晚上好,

我更喜欢有一个简单的扩展方法:

Date.prototype.startOfWeek = function (pStartOfWeek) {
    var mDifference = this.getDay() - pStartOfWeek;

    if (mDifference < 0) {
        mDifference += 7;
    }

    return new Date(this.addDays(mDifference * -1));
}

你会注意到这实际上利用了我使用的另一个扩展方法:

Date.prototype.addDays = function (pDays) {
    var mDate = new Date(this.valueOf());
    mDate.setDate(mDate.getDate() + pDays);
    return mDate;
};

现在,如果你的周从周日开始,为pStartOfWeek参数传递一个“0”,如下所示:

var mThisSunday = new Date().startOfWeek(0);

类似地,如果你的周从星期一开始,为pStartOfWeek参数传递一个“1”:

var mThisMonday = new Date().startOfWeek(1);

问候,

一周的第一天/最后一天

为了得到即将到来的一周的第一天,你可以这样使用:

函数getUpcomingSunday() { const date = new date (); const today = date.getDate(); const currentDay = date.getDay(); const newDate =日期。setDate(今天- currentDay + 7); return newDate (newDate); } console.log (getUpcomingSunday ());

或者获得最晚的第一天:

函数getLastSunday() { const date = new date (); const today = date.getDate(); const currentDay = date.getDay(); const newDate =日期。setDate(今天- (currentDay || 7)); return newDate (newDate); } console.log (getLastSunday ());

*根据您所在的时区,一周的开始不必从周日开始,它可以从周五、周六、周一或您的机器设置的任何其他日子开始。这些方法可以解释。

*你也可以像这样使用toISOString方法格式化它:

简单的解决办法,得到一周的第一天。

使用这种解决方案,可以设置任意的星期开始(例如,星期日= 0,星期一= 1,星期二= 2,等等)。

function getBeginOfWeek(date = new Date(), startOfWeek = 1) {
    const result = new Date(date);
    while (result.getDay() !== startOfWeek) {
        result.setDate(result.getDate() - 1);
    }
    return result;
}

解决方案正确地按月包装(由于使用了Date.setDate()) 对于startOfWeek,可以使用与Date.getDay()中相同的常量

以下是我的解决方案:

function getWeekDates(){
    var day_milliseconds = 24*60*60*1000;
    var dates = [];
    var current_date = new Date();
    var monday = new Date(current_date.getTime()-(current_date.getDay()-1)*day_milliseconds);
    var sunday = new Date(monday.getTime()+6*day_milliseconds);
    dates.push(monday);
    for(var i = 1; i < 6; i++){
        dates.push(new Date(monday.getTime()+i*day_milliseconds));
    }
    dates.push(sunday);
    return dates;
}

现在你可以通过返回的数组索引来选择日期。

该函数使用当前毫秒时间减去当前周,如果当前日期是周一,则再减去一周(javascript从周日开始计数)。

function getMonday(fromDate) {
    // length of one day i milliseconds
  var dayLength = 24 * 60 * 60 * 1000;

  // Get the current date (without time)
    var currentDate = new Date(fromDate.getFullYear(), fromDate.getMonth(), fromDate.getDate());

  // Get the current date's millisecond for this week
  var currentWeekDayMillisecond = ((currentDate.getDay()) * dayLength);

  // subtract the current date with the current date's millisecond for this week
  var monday = new Date(currentDate.getTime() - currentWeekDayMillisecond + dayLength);

  if (monday > currentDate) {
    // It is sunday, so we need to go back further
    monday = new Date(monday.getTime() - (dayLength * 7));
  }

  return monday;
}

当一周从一个月延伸到另一个月(也包括几年)时,我对它进行了测试,它似乎可以正常工作。