This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript date creation, can't set the correct month
I have script like:
var year = 2011;
var month = 3;
var day = 1;
var start = new Date(year, month, day, 0,0,0,0);
When it comes time to use start I find that it ends up evaluating to April 4, 2011. Any ideas why the month gets bumped up by 1?
The month starts counting at zero (see Date).
So, January = 0, and December = 11.
Because the only item in a date whose counter starts with a 1 is a date. Its the way that has been defined.
Everything else (day, hour, min) the first item is defined with a 0.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
The month in JavaScript is based on a zero-based index.
January = 0;
February = 1;
...
Skimming the getMonth() docs on MDN will help explain this more fully.
The months on the javascript Date object is 0 indexed.
0 = January
1 = February
2 = March
3 = April
etc.
month starts from 0 to 11.
0 - Jan, 1- Feb, and so on.
The months are zero-based. 0 corresponds to January, 3 to April.
Related
This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 2 years ago.
I am trying to get the month of the given date as a string in javaScript but I get the wrong month of the date displayed.
var d = new Date("Sun Dec 13 2020 08:00:00 GMT+0530");
console.log(d.getMonth());
I get in the console output for month as 11 for some reason.
Solution
The range begins at zero and not at one
So refering the mdn docs
monthIndex Integer value representing the month, beginning with 0 for
January to 11 for December.
The range from getMonth() is 0-11
So the solution is just to add +1 to the result of the getMonth() method
console.log(d.getMonth() +1 );
the count starts from 0 -january, 1-feb,...11-december
just call this and it is done.
function month(){
const today = new Date();
const options = {
month: "long"
};
return today.toLocaleDateString("en-US", options);
}
This question already has answers here:
`date.setMonth` causes the month to be set too high if `date` is at the end of the month
(8 answers)
Closed 3 years ago.
I found it when i set month
Today is 08-31
If i increase month, then today will be 09-30
But it return 10-01
If i decrease month, then it returns 09-01
date = new Date()
console.log( date )
// Fri Aug 31 2018 11: 28: 47 GMT + 0900(한국 표준시)
console.log( date.getMonth() )
// 7
console.log( date.setMonth(date.getMonth() + 1) )
// 1538360927857
console.log( date.toISOString() )
// "2018-10-01T02:28:47.857Z"
console.log( date.setMonth(date.getMonth() - 1) )
//1535768927857
console.log( date.toISOString() )
//"2018-09-01T02:28:47.857Z"
Can anyone explain me why it happens?
When the current date is 08-31, it is not entirely clear what is should mean to set the month to 9 because 09-31 is an invalid date.
That's why at some time someone had to define what setMonth should do in such situations. It seems that the defined behavior differs from the one you expect (you expected the date to become 09-30).
MDN (for setMonth) has some information about this:
The current day of month will have an impact on the behaviour of this method. Conceptually it will add the number of days given by the current day of the month to the 1st day of the new month specified as the parameter, to return the new date.
It also refers to the actual specification which gives the exact algorithm which is used to calculate the result.
The reasons for the specification being as it is now are manifold, usually simplicity of the algorithm as well as the behavior on non-edge-cases are important factors.
A good explanation can be find on MDN
The current day of month will have an impact on the behaviour of this method. Conceptually it will add the number of days given by the current day of the month to the 1st day of the new month specified as the parameter, to return the new date. For example, if the current value is 31st August 2016, calling setMonth with a value of 1 will return 2nd March 2016. This is because in 2016 February had 29 days.
In your case setting setMonth(getMonth()+1) will return October 1st because September has only 30 days. Then, when you tries to decrease month to one you back to September 1st.
Javascript Date object - monthIndex is Zero (0) based, just like arrays(zero indexed).
MDN
This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 4 years ago.
I can get day no via current date like this, it's give me current result
const dateObject = new Date()
console.log(dateObject.getDay())
But When I try to day no of a specific date, I'm not getting actual answer
const dateObject = new Date(2018, 8, 5)
console.log(dateObject.getDay())
I expect 0 , as it's sunday of the week, but I got 3.
What is my fault or misunderstanding here?
Thanks
The monthIndex parameter in the Date constructor zero-based, so 8 is September, not August. getDay then correctly tells you that the fifth of September is a Wednesday.
See MDN for more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters
There are enough bizarre oddities with JS dates that I just use MomentJS whenever I'm dealing with date/time values.
You are wrong with month index. Month mormally start with 0 index.
January = 0
February = 1
March = 2
April = 3
...
...
...
...
So August month's index number is 7.
You answer will be like this:
const dateObject = new Date(2018, 7, 5)
console.log(dateObject.getDay())
In new Date(2018, 8, 5), month starts from 0 and not 1. Hence in your case it is not August but September.
This question already has answers here:
Why does the month argument range from 0 to 11 in JavaScript's Date constructor?
(10 answers)
Closed 5 years ago.
I'm trying to get the day of the week from a manually set date.
var year = 2017;
var month = 10;
var d = new Date(year, month, 1);
var n = d.getDay();
console.log(n);
The above outputs 3, however, the correct day of the 1st of October is Sunday (ie. day 6 in JS terms). What am i doing wrong?
In javascript months start from 0. So your date is not really 1st of October, it is 1st of November, which is Wednesday.
In JavaScript, dates are denoted by the numbers 0-11 (like an array). So October would be 9. Your program is getting the 1st of November, which is a Wednesday.
Hi I have following code that is suppose to extract day,month and year part separately.
$scope.$watch('inlineDatepicker.date', function() {
alert('hey, myVar has changed!' +$scope.inlineDatepicker.date);
alert($scope.inlineDatepicker.date.getDay());
alert($scope.inlineDatepicker.date.getMonth());
alert($scope.inlineDatepicker.date.getUTCFullYear());
});
Problem with the code is I can extract year correctly but day and month do not extract correctly. I tried as well
alert($scope.inlineDatepicker.date.getUTCDady());
alert($scope.inlineDatepicker.date.getUTCMonth())
Still wrong day and month.
Please let me know how I can change it to get correct month and day values. Thanks. Here is the plunker for it.
http://plnkr.co/edit/8v75gsz8ODUrTfu8S0sh?p=preview
To get day of month, use getUTCDate()
Month is zero based, so 0 means January, 11 December
Sample
$scope.inlineDatepicker.date.getUTCDate(); //prints day of month
$scope.inlineDatepicker.date.getUTCMonth() + 1; //prints month, 0 based, so add 1
$scope.inlineDatepicker.date.getUTCFullYear(); //prints 2015
Get day returns the day of the week. Use getDate() to return the day of the month. If you do getMonth(), january is 0 and december is 11.
You can see the reference documents here: http://www.w3schools.com/jsref/jsref_obj_date.asp