简体   繁体   中英

Is this really a bug in the JAVASCRIPT or am i doing something wrong with the Date, datatype

 let d = new Date('2018,7,26'); let c = new Date() c.setDate(d.getDate() + 1) document.write(c); document.write("<br>"); c.setDate(d.getDate() + 5) document.write(c); document.write("<br>"); c.setDate(d.getDate() + 6) document.write(c); document.write("<br>"); c.setDate(d.getDate() + 7) document.write(c); document.write("<br>"); c.setDate(d.getDate() + 8) document.write(c); document.write("<br>"); c.setDate(d.getDate() + 9) document.write(c); document.write("<br>"); c.setDate(d.getDate() + 10) document.write(c); document.write("<br>"); 

The output of running this code:

Fri Jul 27 2018 14:35:17 GMT+0530 (India Standard Time)
Tue Jul 31 2018 14:35:17 GMT+0530 (India Standard Time)
Wed Aug 01 2018 14:35:17 GMT+0530 (India Standard Time)
Sun Sep 02 2018 14:35:17 GMT+0530 (India Standard Time)
Thu Oct 04 2018 14:35:17 GMT+0530 (India Standard Time)
Sun Nov 04 2018 14:35:17 GMT+0530 (India Standard Time)
Thu Dec 06 2018 14:35:17 GMT+0530 (India Standard Time)

This looks like a bug. If today is 2018-07-26 (ie July, 26), then adding 7 days to it should be 2018-08-02 (August, 2nd). But, it is September, 2nd. Then, adding further makes it even worse.

What is the way around this issue? How to increment dates properly in JS ?

When you set c to something more then the number of days in the month it increases the month in the c . The problem with your code is that you do not clear value of c and it keeps increasing current month in c with each setDate that has argument > 31 .

There is no bug in JS. It's just that you misunderstood how your code works. Pay attention to this code.

c.setDate(d.getDate() + 6) // this will set the c date into 2018-08-01
document.write(c);
document.write("<br>");

c.setDate(d.getDate() + 7)
document.write(c);
document.write("<br>");

d.getDate() + 7 should give you the result 2018-08-02 . However, you put it into c variable which the month is set to 8 due to previous setting. Thus, the month gets increment again and the result is 2018-09-02 and it applies to the rest of code.

This is the fixed code to get your desired result.

var arr = [1, 5, 6, 7, 8, 9, 10]; // you can edit this line according to your needs
for (var i = 0; i < arr.length; i++) {
    let c = new Date();
    c.setDate(d.getDate() + arr[i]);
    document.write(c);
    document.write("<br>");
}

As stated on MDN

If the dayValue is outside of the range of date values for the month, setDate() will update the Date object accordingly. For example, if 0 is provided for dayValue, the date will be set to the last day of the previous month.

If a month has 31 days and you will set the day to 33, it will go to the next month + 2, so August 2nd in this case.

The problem here is that if you again add 34 days, it will stay on the month it incremented to at the previous setDate() and again add one month + the days it has more than that month has. (august has 31 days, so it will set to the 3th).

So to take your example:

// let d = new Date('2018,7,26');
// d.getDate will always return 26

let c = new Date();
c.setDate(27); // works fine, month has 31 days
c.setDate(31); // same
c.setDate(32); // will go to next month + 1, so august 1st
c.setDate(33); // we're at august, august has 31 days so august + 1 month + 2 days = september 2nd
c.setDate(34); // september has 30 days goes to October + 4 days = October 4th

To properly keep adding days you can clone the date:

 let d = new Date('2018-07-26'); let c = new Date() let e; e = new Date(c.getTime()) e.setDate(d.getDate() + 1); console.log(e); e = new Date(c.getTime()) e.setDate(d.getDate() + 5); console.log(e); e = new Date(c.getTime()) e.setDate(d.getDate() + 6); console.log(e); e = new Date(c.getTime()) e.setDate(d.getDate() + 7); console.log(e); e = new Date(c.getTime()) e.setDate(d.getDate() + 8); console.log(e); e = new Date(c.getTime()) e.setDate(d.getDate() + 9); console.log(e); e = new Date(c.getTime()) e.setDate(d.getDate() + 10); console.log(e); 

Also do note as Deceze mentioned in his comment , the date format you used in for creating the date does not work for every browser. Rather use something like 2018-07-26 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM