-4

Date is april 1 2000 but getMonth says it is 3 (march)

I have an object with a date property. I set the date to 01 April 2000 and can see in the debugger that it is set properly to the same date. However when I do a getMonth() on the same date object it returns month as 3 ( March ) . Why is this happening. Does it have anything to do with UTC or Localization both of which i am not using ?

Pradeepl
  • 198
  • 1
  • 12
  • 2
    Months are zero-based in JavaScript. January is `0`, April is `3`. – Robby Cornelissen Apr 17 '18 at 03:35
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth – Daniel W Strimpel Apr 17 '18 at 03:36
  • 2
    Possible duplicate of [why does javascript getMonth count from 0 and getDate count from 1?](https://stackoverflow.com/questions/15799514/why-does-javascript-getmonth-count-from-0-and-getdate-count-from-1) – Robby Cornelissen Apr 17 '18 at 03:41
  • `let mm=this.candidateInfo.dateOfBirth.getMonth();` `mm=mm+1`; – kumbhani bhavesh Apr 17 '18 at 03:42
  • Thanks for the answers. Folks who downvoted, this was a genuine question. I was not aware that in JS months are 0 based and there was no way I would have hit the other question without this knowledge. – Pradeepl Apr 17 '18 at 03:55
  • 1
    Downvotes are also likely because you have made debugging hard by posting code as an image. Post code as code, we can then copy and paste as required. – Jon P Apr 17 '18 at 04:05
  • Got it. Thanks @Jon P. I was trying to show the debug tip :-) – Pradeepl Apr 17 '18 at 07:14

2 Answers2

1

You need +1 for getMonth function var month = date.getMonth() + 1

Demon Spear
  • 104
  • 4
0

this is normal behavior. Months starts with Zero(i.e january is 0). thats why its giving 3 for april. use adding 1 with month to get exact month's digit value.

i guess, to help indexing javascript starts month with zero. suppose an array of months in string form then we dont have to worry to fetch correct month from string.

Jake
  • 141
  • 1
  • 8