admin管理员组

文章数量:1129108

I need the fastest way to get the first day of the week. For example: today is the 11th of November, and a Thursday; and I want the first day of this week, which is the 8th of November, and a Monday. I need the fastest method for MongoDB map function, any ideas?

I need the fastest way to get the first day of the week. For example: today is the 11th of November, and a Thursday; and I want the first day of this week, which is the 8th of November, and a Monday. I need the fastest method for MongoDB map function, any ideas?

Share Improve this question edited Jan 5, 2019 at 9:13 Evan Honnold 256 bronze badges asked Nov 11, 2010 at 16:06 INsINs 3,1492 gold badges21 silver badges26 bronze badges 4
  • If every little bit of speed is crucial, you may want to performance test my answer. I'm getting a little better performance with mine in browsers (except for IE, which favors CMS). Of course, you would need to test it with MongoDB. When the function gets passed a date that is Monday, it should be even faster, since it just returns the unmodified original date. – user113716 Commented Nov 11, 2010 at 17:06
  • I got the same problem and because javascript date object have a lot of bugs I'm using now datejs.com (here code.google.com/p/datejs), a library that correct the miss behavior of the native date. – lolol Commented Oct 2, 2012 at 20:46
  • The question title asks for the first day of the week while the question description asks for the date of the last Monday. These are actually two different questions. Check my answer that solves them both in a correct manner. – Louis Ameline Commented Aug 19, 2018 at 14:08
  • 2 To set a Date to Monday at the start of the week is simply date.setDate(date.getDate() - (date.getDay() || 7) + 1). – RobG Commented Aug 23, 2022 at 8:52
Add a comment  | 

22 Answers 22

Reset to default 436

Using the getDay method of Date objects, you can know the number of day of the week (being 0=Sunday, 1=Monday, etc).

You can then subtract that number of days plus one, for example:

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

console.log( getMonday(new Date()) ); // e.g. Mon Nov 08 2010

Not sure how it compares for performance, but this works.

var today = new Date();
var day = today.getDay() || 7; // Get current day number, converting Sun. to 7
if (day !== 1) // Only manipulate the date if it isn't Mon.
  today.setHours(-24 * (day - 1)); // Set the hours to day number minus 1
//   multiplied by negative 24
console.log(today); // will be Monday

Or as a function:

# modifies _date_
function setToMonday( date ) {
    var day = date.getDay() || 7;  
    if( day !== 1 ) 
        date.setHours(-24 * (day - 1)); 
    return date;
}

setToMonday(new Date());

CMS's answer is correct but assumes that Monday is the first day of the week.
Chandler Zwolle's answer is correct but fiddles with the Date prototype.
Other answers that add/subtract hours/minutes/seconds/milliseconds are wrong because not all days have 24 hours.

The function below is correct and takes a date as first parameter and the desired first day of the week as second parameter (0 for Sunday, 1 for Monday, etc.). Note: the hour, minutes and seconds are set to 0 to have the beginning of the day.

function firstDayOfWeek(dateObject, firstDayOfWeekIndex) {

    const dayOfWeek = dateObject.getDay(),
        firstDayOfWeek = new Date(dateObject),
        diff = dayOfWeek >= firstDayOfWeekIndex ?
            dayOfWeek - firstDayOfWeekIndex :
            6 - dayOfWeek

    firstDayOfWeek.setDate(dateObject.getDate() - diff)
    firstDayOfWeek.setHours(0,0,0,0)

    return firstDayOfWeek
}

// August 18th was a Saturday
let lastMonday = firstDayOfWeek(new Date('August 18, 2018 03:24:00'), 1)

// outputs something like "Mon Aug 13 2018 00:00:00 GMT+0200"
// (may vary according to your time zone)
document.write(lastMonday)

本文标签: algorithmJavaScriptget the first day of the week from current dateStack Overflow