Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript date returns wrong month if day is 01

I am trying to get correct month from the date string, this works fine as long as the day isn't the first of the month (01). If the day is the first, it returns the previous month:

<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the month.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<script>
function myFunction() {
    var str="2014-12-01"
    var d = new Date(str); 
    var m = d.getMonth()+1;
    document.getElementById("demo").innerHTML = m;
}
</script>

</body>
</html>

Returns: 11 Should return: 12

If the date string was 2013-8-01 then 7 would be returned, when it should be 8. Without the +1 after the getMonth() then 6 would be returned, instead of 7.

like image 978
emily Avatar asked Aug 13 '14 14:08

emily


People also ask

Why is getMonth 0 based?

This is because the getMonth method returns a zero-based number between 0 and 11 . For example, January is 0 , February is 1 , March is 2 , etc. If you need to get a one-based value for the month, simply add 1 to the result. Copied!

How to set month from date in JavaScript?

The setMonth() method sets the month of a date object. Note: January is 0, February is 1, and so on. This method can also be used to set the day of the month.

Which method will return the date within a month JavaScript?

Javascript date getMonth() method returns the month in the specified date according to local time. The value returned by getMonth() is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

How to format string date in JavaScript?

The JavaScript toDateString() method returns the date portion of a date object in the form of a string using the following format: First three letters of the week day name. First three letters of the month name. Two digit day of the month, padded on the left a zero if necessary.


2 Answers

The actual problem lies within the timezone of your computer.

Suppose that your computer is in Eastern Time (GMT-5):

var foo = new Date('2014-12-01');

foo.toUTCString(); // "Mon, 01 Dec 2014 00:00:00 GMT"
foo.toISOString(); // "2014-12-01T00:00:00.000Z"
foo.toString(); // "Sun Nov 30 2014 19:00:00 GMT-0500 (Eastern Standard Time)"

Notice that the date is actually in November because it's a few hours behind, and therefore the zero-indexed month would be 10. JavaScript automatically assumes UTC when you do not provide a time string.

The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year.

In local time, this Date object represents November 30 at 19h00 (7pm), so getMonth() returns 10:

foo.getMonth(); // 10
foo.getUTCMonth(); // 11

If you are not concerned about time zones and are just dealing with dates, perhaps look into using the getUTC* methods.

You can read more about the Date object here. Hope this helps.

like image 145
rink.attendant.6 Avatar answered Sep 21 '22 18:09

rink.attendant.6


JavaScript is doing what it should do. In the day value it interprets the 01 as 0, and a zero value for the day is interpreted as the last day of the previous month. It's like asking for day 32 in a month with 31 days. That would return a date of the first of the next month.

like image 26
j08691 Avatar answered Sep 21 '22 18:09

j08691