0

I have a date object and I need to create another date object that is 1 week after the first date object. I already have an implementation but it seems that there is a bug with javascript when it reaches the months of October, November and December. Is there a workaround for this? Note that the behavior is consistent across Chrome, FF and IE.

        // ************ TEST#1 ************
        var startDate = new Date(2011,08,05); // set to Sept 5, 2011
        alert('START DATE' + startDate);

        var endDate = new Date();
        endDate.setDate(startDate.getDate() + 7);

        alert('END DATE' + endDate); // endDate is Sept 12 which is correct
        // check that startDate's value is unchanged
        alert('START DATE' + startDate); 


        // ************ TEST#2 ************
        var startDate = new Date(2011,10,05); // set to Nov 5, 2011
        alert('START DATE' + startDate);

        var endDate = new Date();
        endDate.setDate(startDate.getDate() + 7);

        alert('END DATE' + endDate); // endDate is Sept 12, 2011 which is wrong
        alert('START DATE' + startDate);



        // ************ TEST#3 ************
        // changed implementation but this won't work
        var startDate = new Date(2011,10,05);
        alert('START DATE' + startDate);

        var endDate = startDate;
        endDate.setDate(startDate.getDate() + 7);

        alert('END DATE' + endDate); // endDate is correct but...
        alert('START DATE' + startDate); // startDate's value has changed as well
4
  • 3
    Your first assumption should not be "it's a bug", but rather "what have I done wrong?". Commented Sep 9, 2011 at 13:53
  • 1
    After all those years of envelopment and millions of developers, you think that you will find such a bug? Commented Sep 9, 2011 at 13:54
  • 1
    First rule of programming: It's always your fault Commented Sep 9, 2011 at 13:56
  • You're right. So what have I done wrong? =) Commented Sep 9, 2011 at 13:56

3 Answers 3

3

I think your error might be that you are setting endate to today.

    // ************ TEST#2 ************
    var startDate = new Date(2011,10,05); // set to Nov 5, 2011
    alert('START DATE' + startDate);

    // edit
    var endDate = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate() + 7);
    // old var endDate = new Date();
    // endDate.setDate(startDate.getDate() + 7);

    alert('END DATE' + endDate); // endDate is Sept 12, 2011 which is wrong
    alert('START DATE' + startDate);
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Jonathan. I cannot hard code endDate. My goal is for endDate to be 1 week after startDate, whatever startDate's value is.
@Aleks I've edited it to what I believe will do what your are looking for. Let me know if it works for you.
Works! (but +1 in month field is not needed). Thanks!
@Aleks Yep I though since the month was zero based it would need a +1 but then when i tested it my self I relized the the date field takes the zero based array, so i edited my awnser again and removed the +1 to the month. I tested the end of the month as well. if you change the date to the 30th it will correctly roll to the next month.
2

This is not a bug. In this case Date is an object and both startDate and endDate refer to the same Date instance. Hence when you change the underlying object it's visible through both references

EDIT

OP specified the bug is in Test #2

This is still not a bug. The problem here is that setDate will only change the day of the month. In this you've executed startDate.getDate() + 7 where startDate.getDate() === 5 so it's correctly adjusting the date portion of endDate to the 12th of the month.

1 Comment

I understand this but I forgot to add that TEST#3 is just a different implementation to achieve my goal of having two date objects that is a week apart. The bug is actually in TEST#2.
1

In addition to JaredPar, new Date() will create a date with current time, and if you only invoke .setDate() you will only change "day of month".

2 Comments

I see. My fault is misunderstanding the behavior of setDate. What should I have done in this case?
You can always instantiate a new date based on another date using the constructor: var x = new Date(oldDate); x.setDate(x.getDate() + 7); However, you are better of adding time as milliseconds: var x = new Date(oldDate.valueOf() + 7 * 86400 * 1000); 7 days times 86400 seconds per day times 1000 to get milliseconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.