0

In below code when I subtract day from date I get day as well as month subtracted by one.

var today = new Date();

today.setDate(today.getDate() - 1);

var dd = today.getDate();
var mm = today.getMonth() + 1;  //Here is the problem so I have to do +1 to get to current month
var yyyy = today.getFullYear(); 

Anyone any idea why does this happen? Or if I am doing it in the wrong way can anyone suggest the right way?

c69
  • 19,951
  • 7
  • 52
  • 82
Er. ßridy
  • 553
  • 2
  • 6
  • 20

3 Answers3

2

with javascript's date.getMonth() method, months are 0-11, there is nothing wrong here

birdspider
  • 3,034
  • 1
  • 16
  • 25
0

Oh ya I just saw the details in http://www.w3schools.com/jsref/jsref_getmonth.asp

The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.

Thank you for the information guys!

Er. ßridy
  • 553
  • 2
  • 6
  • 20
  • Glad you solved it...according to StackOverflow guidelines though, this comment should be edited into your question rather than posted as an answer. And you should upvote and accept @birdspider's answer if it was helpful to you. – Matt Browne Aug 08 '14 at 11:08
0

You've got confused.

.getDate returns day of the month, from 1 to 31.

But .getMonth returns month index, starting from 0 (from 0 to 11).

Blame Java legacy for that ;)

c69
  • 19,951
  • 7
  • 52
  • 82