TransWikia.com

Why is getDate() a month off?

Stack Overflow Asked by aztheog on December 21, 2020

I’m trying to iterate over objects that have a date within a specific time frame. These time frames are days in the past week bounded by startDate and endDate.

I have a for loop that sets the startDate to the beginning of the day, and endDate will be the end of the day.

let startDate = new Date(); // startDate: Start bound of average execution time calculation
let endDate = new Date(); // endDate: End bound of average execution time calculation

for (let i = 0; i < 7; i++) {
          // i is used to decrement the startDate to i days from today

          // set startDate to beginning of the day
          startDate = new Date();
          startDate.setDate(startDate.getDate() - i);
          startDate.setHours(0, 0, 0, 0);
          console.log(i + ": " + startDate);

          // set endDate to end of the day
          endDate.setDate(startDate.getDate());
          endDate.setHours(23, 59, 59, 999);
          console.log(i + ": " + endDate);

However, even though I’m setting the endDate to the startDate, the console tells me that the endDate is one month off. Why is this happening?

The console shows this:

0: Sat Aug 01 2020 00:00:00 GMT-0700 (Mountain Standard Time)
0: Sat Aug 01 2020 23:59:59 GMT-0700 (Mountain Standard Time)
1: Fri Jul 31 2020 00:00:00 GMT-0700 (Mountain Standard Time)
1: Mon Aug 31 2020 23:59:59 GMT-0700 (Mountain Standard Time)
2: Thu Jul 30 2020 00:00:00 GMT-0700 (Mountain Standard Time)
2: Sun Aug 30 2020 23:59:59 GMT-0700 (Mountain Standard Time)
3: Wed Jul 29 2020 00:00:00 GMT-0700 (Mountain Standard Time)
3: Sat Aug 29 2020 23:59:59 GMT-0700 (Mountain Standard Time)
4: Tue Jul 28 2020 00:00:00 GMT-0700 (Mountain Standard Time)
4: Fri Aug 28 2020 23:59:59 GMT-0700 (Mountain Standard Time)
5: Mon Jul 27 2020 00:00:00 GMT-0700 (Mountain Standard Time)
5: Thu Aug 27 2020 23:59:59 GMT-0700 (Mountain Standard Time)

3 Answers

The reason it happens is because you are setting the date but not the month, so when the loop crosses a month boundary, the start goes to the previous month but end stays in the current month.

When run on 1 Aug, in the first iteration both start and end are set to 1 Aug.

In the next iteration, 1 is subtracted from the start so it's 31 July, but then just the date is set for end, so it goes to 31 Aug, and so on…

A reworking of the code:

// Start at random time on 1 Aug
let d = new Date(2020,7,1,15,23,51,3);

for (let start, end, i=0; i<7; i++) {
  start = new Date(d.setHours(0,0,0,0));
  console.log(i + ' : ' + start.toString());
  end = new Date(d.setHours(23,59,59,999));
  console.log(i + ' : ' + end.toString());
  d.setDate(d.getDate() - 1);
}

Answered by RobG on December 21, 2020

So, I ended up just fixing it by not referring to startDate at all. I'm not sure why I can't call getDate() without getting the wrong date, but for now, I'll just set endDate by repeating whatever I did to startDate.

// set startDate to beginning of the day
startDate = new Date();
startDate.setDate(startDate.getDate() - i);
startDate.setHours(0, 0, 0, 0);
console.log(i + ": " + startDate);

// set endDate to end of the day
endDate = new Date();
endDate.setDate(endDate.getDate() - i);
endDate.setHours(23, 59, 59, 59);
console.log(i + ": " + endDate);

Answered by aztheog on December 21, 2020

Think like arrays, where the first element is 0. The same is true here. The months of the year will be 0 - 11, rather than 1 - 12. The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).

Also, keep this in mind: With setDate() the expected values are 1-31, but other values are allowed: 0 will result in the last day of the previous month. -1 will result in the day before the last day of the previous month.

Answered by Muirik on December 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP