I am trying to get a month from a date string, this works fine until the day is the first month (01). If the day is 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>
Refund: 11 Must Return: 12
If the date string was 2013-8-01, then 7 will be returned, when it should be 8. Without "+1" after "getMonth ()" then 6, not 7 will be returned.
emily source
share