How to get the first and last day of the week in JavaScript

I have an object today = new Date(); . I need to get the first and last day of the current week. I need both options for Sunday and Monday as the beginning and end of the day of the week. Now I am a bit confused with the code. Can you help me?

+52
javascript date
Mar 06 '11 at 12:10
source share
16 answers
 var curr = new Date; // get current date var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week 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(); firstday "Sun, 06 Mar 2011 12:25:40 GMT" lastday "Sat, 12 Mar 2011 12:25:40 GMT" 

This works for the first day = Sunday of this week and last day = Saturday this week. Extending it to work Monday through Sunday is trivial.

Working with the first and last days in different months remains as an exercise for the user

+121
Mar 06 '11 at 12:27
source share

Be careful with the accepted answer, it does not set the time until 00:00:00 and 23:59:59, so you may have problems.

I recommend using Moment.js for working with dates. For your case:

 var startOfWeek = moment().startOf('week').toDate(); var endOfWeek = moment().endOf('week').toDate(); 

This is just a small use case, it is very simple to perform many complex operations.

You can find out more here: http://momentjs.com/

+31
Nov 14 '14 at 2:39 on
source share

You can also use the following lines of code to get the first and last date of the week:

 var curr = new Date; var firstday = new Date(curr.setDate(curr.getDate() - curr.getDay())); var lastday = new Date(curr.setDate(curr.getDate() - curr.getDay()+6)); 

Hope this will be helpful.

+29
Nov 02 '12 at 7:11
source share

Here is a quick way to get the first and last day for any starting day. knowing that:

1 day = 86,400,000 milliseconds.

JS date values ​​are in milliseconds

Recipe: Find out how many days you need to remove in order to get the day of the beginning of the week (multiply by 1 day in milliseconds). All that remains after this is to add 6 days to get the final day.

 var startDay = 1; //0=sunday, 1=monday etc. var d = now.getDay(); //get the current day var weekStart = new Date(now.valueOf() - (d<=0 ? 7-startDay:d-startDay)*86400000); //rewind to start day var weekEnd = new Date(weekStart.valueOf() + 6*86400000); //add 6 days to get last day 
+15
Feb 20 2018-12-12T00:
source share

I recommend using Moment.js for such cases. I had scenarios where I had to check the current time, this week, this month and at this time. The above answer helped me, so I decided to share the rest of the features.

Just to get the current time in a specific format

  case 'Today': moment().format("DD/MM/YYYY h:mm A"); case 'This Week': moment().endOf('isoweek').format("DD/MM/YYYY h:mm A"); 

A week starts on Sunday and ends on Saturday, if we just use the "week" as a parameter for the endOf function, but to get Sunday as the end of the week, we need to use "isoweek".

  case 'This Month': moment().endOf('month').format("DD/MM/YYYY h:mm A"); case 'This Quarter': moment().endOf('quarter').format("DD/MM/YYYY h:mm A"); 

I chose this format according to my need. You can change the format to suit your requirements.

+5
Nov 18 '15 at 17:07
source share

It works throughout the year and month.

 Date.prototype.GetFirstDayOfWeek = function() { return (new Date(this.setDate(this.getDate() - this.getDay()))); } Date.prototype.GetLastDayOfWeek = function() { return (new Date(this.setDate(this.getDate() - this.getDay() +6))); } var today = new Date(); alert(today.GetFirstDayOfWeek()); alert(today.GetLastDayOfWeek()); 
+3
Nov 20 '15 at 19:02
source share

You can do something like this

 var today = new Date(); var startDay = 0; var weekStart = new Date(today.getDate() - (7 + today.getDay() - startDay) % 7); var weekEnd = new Date(today.getDate() + (7 - today.getDay() - startDay) % 7); 

Where startDay is a number from 0 to 6, where 0 means Sunday (i.e. 1 = Monday, 2 = Tuesday, etc.).

+2
Mar 06 2018-11-11T00:
source share

The krtek method has some errors, I tested this

 var startDay = 0; var weekStart = new Date(today.getDate() - (7 + today.getDay() - startDay) % 7); var weekEnd = new Date(today.getDate() + (6 - today.getDay() - startDay) % 7); 

works

+2
Jun 27 '13 at 3:25
source share

SetDate sets the day of the month. Using SetDate during the start and end of the month will lead to the wrong week

 var curr = new Date("08-Jul-2014"); // get current date var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week var last = first + 6; // last day is the first day + 6 var firstday = new Date(curr.setDate(first)); // 06-Jul-2014 var lastday = new Date(curr.setDate(last)); //12-Jul-2014 

If u sets Date 01-Jul-2014, it will be shown on the first day as June 29, 2014, and on the last day from June 05, 2014, and not from July 05, 2014.

So let's get over this problem, I used

 var curr = new Date(); day = curr.getDay(); firstday = new Date(curr.getTime() - 60*60*24* day*1000); //will return firstday (ie sunday) of the week lastday = new Date(curr.getTime() + 60 * 60 *24 * 6 * 1000); //adding (60*60*6*24*1000) means adding six days to the firstday which results in lastday (saturday) of the week 
+2
Jul 08 '14 at 6:37
source share

Good suggestion, but you had a little problem last day. You must change it to:

 lastday = new Date(firstday.getTime() + 60 * 60 *24 * 6 * 1000); 
+1
May 27 '15 at 8:17
source share

Although the question looks outdated, I have to point out the problem.
Question: What will happen from January 1, 2016?
I think most of the above solutions calculate the start of the week as 12/27/2016. For this reason, I think the correct calculation should be as shown below:

 var d = new Date(), dayInMs = 1000 * 60 * 60 * 24, weekInMs = dayInMs * 7, startOfToday = new Date(d.getFullYear(), d.getMonth(), d.getDate()).valueOf(), todayElapsedTime = d.valueOf() - startOfToday, dayDiff = d.getDay() * dayInMs, dateDiff = dayDiff + todayElapsedTime, // finally startOfWeek = d.valueOf() - dateDiff, endOfWeek = startOfWeek + weekInMs - 1; 
+1
03 Sep '15 at 15:37
source share
  //get start of week; QT function _getStartOfWeek (date){ var iDayOfWeek = date.getDay(); var iDifference = date.getDate() - iDayOfWeek + (iDayOfWeek === 0 ? -6:1); return new Date(date.setDate(iDifference)); }, function _getEndOfWeek(date){ return new Date(date.setDate(date.getDate() + (7 - date.getDay()) === 7 ? 0 : (7 - date.getDay()) )); }, 

* current date == 06/30/2016 and Monday - the first day of the week.

He also works for several months and years. Tested with qunit suite:

enter image description here

  QUnit.module("Planung: Start of week"); QUnit.test("Should return start of week based on current date", function (assert) { var startOfWeek = Planung._getStartOfWeek(new Date()); assert.ok( startOfWeek , "returned date: "+ startOfWeek); }); QUnit.test("Should return start of week based on a sunday date", function (assert) { var startOfWeek = Planung._getStartOfWeek(new Date("2016-07-03")); assert.ok( startOfWeek , "returned date: "+ startOfWeek); }); QUnit.test("Should return start of week based on a monday date", function (assert) { var startOfWeek = Planung._getStartOfWeek(new Date("2016-06-27")); assert.ok( startOfWeek , "returned date: "+ startOfWeek); }); QUnit.module("Planung: End of week"); QUnit.test("Should return end of week based on current date", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date()); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on sunday date with different month", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-07-03")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on monday date with different month", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-06-27")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on 01-06-2016 with different month", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-06-01")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on 21-06-2016 with different month", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-06-21")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on 28-12-2016 with different month and year", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-12-28")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on 01-01-2016 with different month and year", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-01-01")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); 
+1
Jun 30 '16 at 11:47
source share
 var dt = new Date() //current date of week var currentWeekDay = dt.getDay(); var lessDays = currentWeekDay == 0 ? 6 : currentWeekDay-1 var wkStart = new Date(new Date(dt).setDate(dt.getDate()- lessDays)); var wkEnd = new Date(new Date(wkStart).setDate(wkStart.getDate()+6)); 

This will be useful for any date scenario.

+1
May 03 '17 at 5:26 a.m.
source share

The excellent (and immutable) data-fns library handles this most succinctly:

 const start = startOfWeek(date); const end = endOfWeek(date); 
+1
Jun 06 '17 at 14:13
source share

The instant approach worked for me in all cases (although I did not test boundaries such as the end of the year, leap years). Only the fix in the above code is an isoWeek parameter if you want to start the week from Monday.

  let startOfWeek = moment().startOf("isoWeek").toDate(); let endOfWeek = moment().endOf("isoWeek").toDate(); 
0
Jun 04 '17 at 23:22
source share

Just using pure javascript, you can use the function below to get the first and last day of the week with free day assignment for the start of the week.

 var weekday = []; weekday[0] = "Sunday"; weekday[1] = "Monday"; weekday[2] = "Tuesday"; weekday[3] = "Wednesday"; weekday[4] = "Thursday"; weekday[5] = "Friday"; weekday[6] = "Saturday"; function getFirstDayOfWeek(date, from) { //Default start week from 'Sunday'. You can change it yourself. from = from || 'Sunday'; var index = weekday.indexOf(from); var start = index >= 0 ? index : 0; var d = new Date(date); var day = d.getDay(); var diff = d.getDate() - day + (start > day ? start - 7 : start); d.setDate(diff); return d; }; 

The last day of the week - just 6 days after the first day of the week.

 function getLastDayOfWeek(date, from) { from = from || 'Sunday'; var index = weekday.indexOf(from); var start = index >= 0 ? index : 0; var d = new Date(date); var day = d.getDay(); var diff = d.getDate() - day + (start > day ? start - 1 : 6 + start); d.setDate(diff); return d; }; 

Test:

 getFirstDayOfWeek('2017-10-16'); //--> Sun Oct 15 2017 getFirstDayOfWeek('2017-10-16', 'Monday'); //--> Mon Oct 16 2017 getFirstDayOfWeek('2017-10-16', 'Tuesday'); //--> Tue Oct 10 2017 
0
Oct 16 '17 at 10:04 on
source share



All Articles