1

No clue why I'm getting two different dates for a code below

        var aDate = '12/31/2014';
        var bDate = new Date(aDate);
        var cDate = bDate.setMonth(bDate.getMonth() + 3);
        var calDate = new Date(cDate);
        var yc = calDate.getFullYear(), mc = calDate.getMonth(), dc = calDate.getDate();
        alert(calDate + ' ' + mc+'/'+dc+'/'+yc); 

For me mc/dc/yc should be same day as calDate

JSFiddle

1
  • 1
    You're forgetting that javascript months are zero-based. JS month #2 is real-world month #3 Commented Dec 11, 2014 at 21:56

1 Answer 1

3

Month is 0-based:

mc = calDate.getMonth()+1
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that is interesting
OK, found it: 0=January, 1=February etc.

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.