0

For example, in the case of 03/27/2016 to 04/02/2016, the dates fall in different months.

var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay();
var last = first + 6; // last day is the first day + 6

var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();
isherwood
  • 58,414
  • 16
  • 114
  • 157
anna
  • 93
  • 3
  • 13

4 Answers4

1

The getDay method returns the number of the day in the week, with Sunday as 0 and Saturday as 6. So if your week starts on Sunday, just subtract the current day number in days from the current date to get the start, and add 6 days get the end, e.g.

function getStartOfWeek(date) {
  
  // Copy date if provided, or use current date if not
  date = date? new Date(+date) : new Date();
  date.setHours(0,0,0,0);
  
  // Set date to previous Sunday
  date.setDate(date.getDate() - date.getDay());
  
  return date;
}

function getEndOfWeek(date) {
  date = getStartOfWeek(date);
  date.setDate(date.getDate() + 6);
  return date; 
}
  
document.write(getStartOfWeek());

document.write('<br>' + getEndOfWeek())

document.write('<br>' + getStartOfWeek(new Date(2016,2,27)))

document.write('<br>' + getEndOfWeek(new Date(2016,2,27)))
RobG
  • 142,382
  • 31
  • 172
  • 209
1

You can find below solution for your problem.

let currentDate = new Date; // get current date
let first = currentDate.getDate() - currentDate.getDay();
var last = first + 6; // last day is the first day + 6
let firstDayWeek = new Date(currentDate.setDate(first)).toISOString();
var lastDayWeek = new Date(currentDate.setDate(last)).toISOString();
console.log(firstDayWeek, "first Day in week")
console.log(lastDayWeek, "end Day in week")     
Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
0

I like the moment library for this kind of thing:

moment().startOf("week").toDate();
moment().endOf("week").toDate();
Shaun
  • 2,012
  • 20
  • 44
  • is there any way to do it in javascript without moment. – anna Mar 29 '16 at 19:10
  • I think you would have to do something like Zarana recommended, converting the date to its integer value and working with it as a number. – Shaun Mar 29 '16 at 19:32
  • Answers should include an explanation and should not require a library that isn't mentioned in the question or tagged. – RobG Mar 29 '16 at 22:48
  • @RobG It seems like that is up for debate. http://meta.stackexchange.com/questions/40897/is-it-okay-to-post-answers-using-libraries-frameworks-extensions-not-already-ment – Shaun Mar 30 '16 at 13:35
  • @Shaun–the "no" response has many more votes than "yes". It's fine to include a library answer, but the answer should not require a library that isn't asked for and should also offer an explanation. – RobG Mar 30 '16 at 22:56
0

You can try this:

var currDate = new Date();
day = currDate.getDay();
first_day = new Date(currDate.getTime() - 60*60*24* day*1000); 
last_day = new Date(currDate.getTime() + 60 * 60 *24 * 6 * 1000);
randomstudious
  • 122
  • 2
  • 10
  • To get the value for the last day of the week, you will have to subtract the day value from the maximum week days: `last_day = new Date(currDate.getTime() + 60 * 60 *24 * (6 - day) * 1000);` – Shaun Mar 29 '16 at 19:32