2

I have this code where I get the current week's first and last day (starting from sunday - ending in saturday). It was working fine this week until the month changed today.

Here is my code:

// current date 1 Sep 2017
curr = new Date(2017, 8, 1);

console.log(curr.toString());
// Fri Sep 01 2017 00:00:00


// First day of week
var first = curr.getDate() - curr.getDay();

// Last day week
var last = first + 6;

var startDate = new Date(curr.setDate(first));
console.log(startDate.toString());
// Sun Aug 27 2017 00:00:00

var endDate = new Date(curr.setDate(last));
console.log(endDate.toString());
// Wed Aug 02 2017 00:00:00

So the problem is, that the endDate should be Sat Sep 02 not Wed Aug 02

This was working fine until the month changed from Aug to Sep.

Any ways to fix this?

RobG
  • 142,382
  • 31
  • 172
  • 209
IlariM
  • 376
  • 1
  • 3
  • 16
  • seems to be correct. what browser are you using? – blurfus Aug 31 '17 at 23:41
  • @ochi endDate showing as Wed Aug 02 when it should be Sat Sep 02 – IlariM Sep 01 '17 at 00:13
  • 1
    Not all days are 24hrs long in places where daylight saving is observed. Adding a day using `60 * 60 * 24 * 7` is not sensible, see [*Add +1 to current date*](https://stackoverflow.com/questions/9989382/add-1-to-current-date/9989458#9989458). Also, you should probably set the time to 00:00:00. – RobG Sep 01 '17 at 00:24
  • did you see the answer by @Will ? - I see `Thu Aug 31 2017 17:44:31 GMT-0700 (Pacific Daylight Time) Sun Aug 27 2017 17:44:31 GMT-0700 (Pacific Daylight Time) Sat Sep 02 2017 17:44:31 GMT-0700 (Pacific Daylight Time)` – blurfus Sep 01 '17 at 00:46
  • @ochi I have `Wed Aug 02 2017 04:10:54 GMT+0300` as `endDate`. So it's just because of the timezone difference? – IlariM Sep 01 '17 at 01:01
  • maybe, or browser/JS engine? which is why I asked what browser you were using. I am on Windows/Chrome. Also tested on Windows/IE and Windows/Firefox – blurfus Sep 01 '17 at 01:03
  • @ochi I'm coding an mobile app with cordova (VS15) and I'm simulating it on chrome. – IlariM Sep 01 '17 at 01:04

2 Answers2

1

I see. The end date isn't getting set correctly. Try this.

let curr = new Date();
console.log(curr.toString());

let startDate = new Date(curr.setDate(curr.getDate() - curr.getDay()));
console.log(startDate.toString());

let endDate = new Date(curr.setDate(curr.getDate() + 6));
console.log(endDate.toString());
Will
  • 3,201
  • 1
  • 19
  • 17
  • It returns Sun 27 Aug and Wed 2 Aug. – RobG Sep 01 '17 at 00:44
  • Gotta be the browser. I see ```Thu Aug 31 2017 17:44:54 GMT-0700 (PDT)``` ```Sun Aug 27 2017 17:44:54 GMT-0700 (PDT)``` ```Sat Sep 02 2017 17:44:54 GMT-0700 (PDT)``` – Will Sep 01 '17 at 00:45
  • It's not the browser (I tested in Safari, Chrome and Firefox). See my answer below for why it fails. It "works" for you because the initial date is in August. Set the initial date to 1 September (or any date where the current date and start of the week are different months), it fails. – RobG Sep 01 '17 at 00:49
0

I think this is a duplicate but I can't find one right now. Your problem is the way you go about adjusting the dates. Date methods modify the Date they're called on, so you are modifying curr as you go but treating it as if it hasn't changed.

// current date
curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);

That's not a good way to add 1 day, but not your issue. Much better to first zero the time then add one to the date.

console.log(curr);
// Fri Sep 01 2017 01:52:15 


// First day of week
var first = curr.getDate() - curr.getDay();

Sets first to -4 (date is 1, Friday is 5).

// Last day week
var last = first + 6;

Sets last to 2 (-4 + 6).

var startDate = new Date(curr.setDate(first));

This modifies curr to be 27 August. It is then copied to create a new Date that is assigned to startDate (i.e. curr and startDate have the same time value and represent the same date and time).

console.log(startDate);
// Sun Aug 27 2017 01:57:25 

Yep. Now you set the date for curr to 2, i.e. 2 August.

var endDate = new Date(curr.setDate(last));
console.log(endDate);
// Wed Aug 02 2017 01:59:37

Yep. As an alternative, copy the original date and modify the copy to get startDate, then get another copy and modify it for the end date.

function getWeekStartEnd(date) {
  // Copy date so don't modify original
  date = date? new Date(+date) : new Date();
  // Set time to 0 and copy date at same time
  var startDate = new Date(date.setHours(0,0,0,0));
  // Set startDate to previous Sunday
  startDate.setDate(startDate.getDate() - startDate.getDay());
  // Set endDate to next Saturday
  var endDate = new Date(+date);
  endDate.setDate(endDate.getDate() + 6 - endDate.getDay());
  return [startDate, endDate];
}

// Current week
getWeekStartEnd().forEach(function(d){
  console.log(d.toString());
});

// For Friday 1 September, 2017
getWeekStartEnd(new Date(2017,8,1)).forEach(function(d){
  console.log(d.toString());
});

// For Sunday 3 September, 2017
getWeekStartEnd(new Date(2017,8,3)).forEach(function(d){
  console.log(d.toString());
});
RobG
  • 142,382
  • 31
  • 172
  • 209