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


当前回答

一周的第一天/最后一天

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

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

其他回答

周一上午00点到周一上午00点返回。

const now = new Date()
const startOfWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay() + 1)
const endOfWeek = new Date(now.getFullYear(), now.getMonth(), startOfWeek.getDate() + 7)

我用这个:

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

它工作得很好。

区分本地时间和UTC时间是很重要的。我想用UTC找到一周的开始,所以我使用了下面的函数。

function start_of_week_utc(date, start_day = 1) {

// Returns the start of the week containing a 'date'. Monday 00:00 UTC is
// considered to be the boundary between adjacent weeks, unless 'start_day' is
// specified. A Date object is returned.

    date = new Date(date);
    const day_of_month = date.getUTCDate();
    const day_of_week = date.getUTCDay();
    const difference_in_days = (
        day_of_week >= start_day
        ? day_of_week - start_day
        : day_of_week - start_day + 7
    );
    date.setUTCDate(day_of_month - difference_in_days);
    date.setUTCHours(0);
    date.setUTCMinutes(0);
    date.setUTCSeconds(0);
    date.setUTCMilliseconds(0);
    return date;
}

要在给定时区中找到一周的开始,首先将时区偏移量添加到输入日期,然后从输出日期中减去时区偏移量。

const local_start_of_week = new Date(
    start_of_week_utc(
        date.getTime() + timezone_offset_ms
    ).getTime() - timezone_offset_ms
);

接受的答案将不适用于在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

查看Date.js

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