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


当前回答

我用这个:

let current_date = new Date();
let days_to_monday = 1 - current_date.getDay();
monday_date = current_date.addDays(days_to_monday);

// https://stackoverflow.com/a/563442/6533037
Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

它工作得很好。

其他回答

查看Date.js

Date.today().previous().monday()

一个只有数学计算的例子,没有任何Date函数。

const date = new date (); Const ts = +日期; const mondayTS = ts % (60 * 60 * 24 * (7-4) * 1000); const monday =新的日期(星期一); console.log(monday.toISOString(), 'Day:', monday.getDay());

const formatTS = v => new Date(v).toISOString(); const adjust = (v, d = 1) => v - v % (d * 1000); const d = new Date('2020-04-22T21:48:17.468Z'); const ts = +d; // 1587592097468 const test = v => console.log(formatTS(adjust(ts, v))); test(); // 2020-04-22T21:48:17.000Z test(60); // 2020-04-22T21:48:00.000Z test(60 * 60); // 2020-04-22T21:00:00.000Z test(60 * 60 * 24); // 2020-04-22T00:00:00.000Z test(60 * 60 * 24 * (7-4)); // 2020-04-20T00:00:00.000Z, monday // So, what does `(7-4)` mean? // 7 - days number in the week // 4 - shifting for the weekday number of the first second of the 1970 year, the first time stamp second. // new Date(0) ---> 1970-01-01T00:00:00.000Z // new Date(0).getDay() ---> 4

晚上好,

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

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);

问候,

接受的答案将不适用于在UTC-XX:XX时区运行代码的任何人。

这里的代码将工作,无论时区仅为日期。如果你也提供时间,这就行不通了。只提供日期或解析日期并将其作为输入。我在代码开始时提到了不同的测试用例。

function getDateForTheMonday(dateString) { var orignalDate = new Date(dateString) var modifiedDate = new Date(dateString) var day = modifiedDate.getDay() diff = modifiedDate.getDate() - day + (day == 0 ? -6:1);// adjust when day is sunday modifiedDate.setDate(diff) var diffInDate = orignalDate.getDate() - modifiedDate.getDate() if(diffInDate == 6) { diff = diff + 7 modifiedDate.setDate(diff) } console.log("Given Date : " + orignalDate.toUTCString()) console.log("Modified date for Monday : " + modifiedDate) } getDateForTheMonday("2022-08-01") // Jul month with 31 Days getDateForTheMonday("2022-07-01") // June month with 30 days getDateForTheMonday("2022-03-01") // Non leap year February getDateForTheMonday("2020-03-01") // Leap year February getDateForTheMonday("2022-01-01") // First day of the year getDateForTheMonday("2021-12-31") // Last day of the year

一周的第一天/最后一天

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

函数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方法格式化它: