-2

getMonth() javascript method returns the month of the Date and values are (0-11) 0 for January and 11 for December.

If today was "2015-03-01" (first day of March 2015), getMonth() should return 2 but instead it returns 1. Why is this? how can I solve this issue?

Help!!

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Date Example</h2>

<p id="demo"></p>

<script>
var x = new Date("2015-03-01");
document.getElementById("demo").innerHTML = x.getMonth();

</script>

</body>
</html>

When I "Run code Snippet" It shows this:

example of result

mplungjan
  • 169,008
  • 28
  • 173
  • 236
toreohm
  • 75
  • 6
  • Seeing it returns 2 in your example and also when doing this in the console `var x = new Date("2015-03-01"); x.getMonth()` What is the issue? – Nope Sep 11 '17 at 16:10
  • Even your sample code snippet here outputs "2". – Tomalak Sep 11 '17 at 16:12
  • Well that's funny, it returns 1 for me when I run code snippet. – toreohm Sep 11 '17 at 16:13
  • Perhaps he is in a different timezone. Try `var x = new Date(2015,2,1,12,0,0,0);` for first of march 2015 – mplungjan Sep 11 '17 at 16:21
  • Concerning my "duplicate question". I have checked the "original question" and yes I already know that dateObject.getMonth()+1 gives you the exact month. However I believe that my question is a different issue as you can see in the screenshot. – toreohm Sep 11 '17 at 17:08
  • 1
    The problem is that an ISO format date string is treated as UTC, so if you're west of Greenwich, "2015-03-01" will be sometime on 28 Feb 2015, so *getMonth* returns 1. If you use *getUTCMonth*, you'll get 2. This is a duplicate of parsing, not month index. – RobG Sep 11 '17 at 23:26
  • @RobG getUTCMonth does work!! Thankyou for help! – toreohm Sep 12 '17 at 17:35

1 Answers1

0

getMonth() method of Date always return result with zero base.So to get exact month number you should always be using .getMonth()+1 like below

<script>
var x = new Date("2015-03-01");
document.getElementById("demo").innerHTML = x.getMonth()+1;

</script>

This will paint the desired result.

Shijil Narayanan
  • 1,011
  • 8
  • 21
  • Thankyou but I already know that. That is not the issue. I edited my question and I added a screenshot to ilustrate my issue. People tell me that when they run my sample code snippet it outputs "2". But when I run the same code snippet it outputs "1". You can click in "example of result" in my edited question to see the screenshot. – toreohm Sep 11 '17 at 16:27
  • @toreohm—this is a duplicate, but of [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) The date string is parsed as UTC, so if you're west of Greenwich the local date will be 28 Feb 2015 and *getMonth* returns 1. Try *getUTCMonth*, it will return 2. – RobG Sep 11 '17 at 23:30
  • @RobG getUTCMonth did work!! Thanks! Since you have gave me the solution, how can I "close" this issue (question)? – toreohm Sep 12 '17 at 17:33
  • @toreohm—I've marked it as a duplicate, so it's "closed". ;-) – RobG Sep 13 '17 at 00:42