getMonth() in Date shows wrong value [duplicate] - javascript

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);
}

Related

Get day number from a specific date [duplicate]

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.

Get week day from specific date set in Date in JS [duplicate]

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.

Javascript getMonth returning previous month after adding days [duplicate]

This question already has answers here:
unexpected javascript date behavior
(3 answers)
Closed 6 years ago.
I am adding days in today date and getting timestamps in milliseconds.
for(i=1;i<=shippingDays;i++){
var result = new Date();
result.setTime( result.getTime() + i * 86400000 );
console.log(result);
console.log(result.getMonth()+'-'+result.getDate()+'-'+result.getFullYear());
newDate = new Date(result.getMonth()+'-'+result.getDate()+'-'+result.getFullYear());
console.log(newDate);
};
The first console returns Thu Apr 07 2016 18:34:33 GMT+0500 (PKT) but later on result.getMonth() always returns previous month value. So the second console always returns 3-7-2016 and third console always returns Mon Mar 07 2016 00:00:00 GMT+0500 (PKT).
My ultimate goal is to get milliseconds of next days from 00:00:00. Like today is 04-06-2016. I want to get milliseconds timestamp of next few days. And time stamp should be calculated from start of that date, i.e, 00:00:00
Can any body let me know what am I doing wrong?
Months from Data.getMonth are zero based (so January is 0, feb is 1, etc).
So if you want to use the month value to make a new date just add one.
getMonth is zero based so 0=January, 1=February, 3=April etc. So the output of console.log is correct
You need to add 1 to the getMonth() function.
Example:
console.log((result.getMonth() + 1)+'-'+result.getDate()+'-'+result.getFullYear());
Will return the correct month / date. It's because the getMonth function returns a value of 0 - 11 (0 for January and 11 for December)
http://www.w3schools.com/jsref/jsref_getmonth.asp

what is the difference between new Date and +new Date? [duplicate]

This question already has answers here:
What does the plus sign do in '+new Date'
(7 answers)
Closed 8 years ago.
What is the difference between new Date and +new Date?
For example:
var date = new Date;
console.log(date);
var plusDate = +new Date;
console.log(plusDate);
Logs:
Sat May 10 2014 01:13:46 GMT+0300 (Jordan Standard Time)
1399673626539
The unary plus operator casts the Date object to a Number object (which is expressed in in milliseconds since 01 January, 1970 UTC).
The first one creates a Date object, the second one adds the current date value in milliseconds to the original value of plusDate, which was 0.

Javascript and the Date() function [duplicate]

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.

Categories