Get day number from a specific date [duplicate] - javascript

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.

Related

getMonth() in Date shows wrong value [duplicate]

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

Weird behaviour from JS Date() object [duplicate]

This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 2 years ago.
I want to represent the first of a given month, say 1st of Septemeber 2020. When I do this in my console, I get the below output:
$ node
> new Date(2020, 8, 1)
2020-08-31T23:00:00.000Z
How come it returns 2020-08-31 instead of 2020-08-01? What am I doing wrong?
The output
2020-08-31T23:00:00.000Z
is ISO format, and the Z shows the date is in UTC. When you call the Date constructor, it is using your browser timezone by default, which I can assume is +01:00. Moreover, month indice in the constructor is zero-based.
If what you want is the beginning of the month in UTC, you can do:
d = new Date(Date.UTC(2020, 8, 01))
d.toISOString()
"2020-09-01T00:00:00.000Z"
try
new Date(2020, 7, 1)
because date object is count from 0-11 no 1-12

Why getMonth returns different values on same date with different date formats [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
The Date.getMonth() returns different values for same date in different formats.
The first statement is in the UTC format.
The second one uses YYYY,M,DD format.
I didn't expect it to return a different value because it's the same date.
What is going on?
console.log(new Date('July 20, 69 00:20:18').getMonth()); // returns 6
console.log(new Date(1969, 7, 20).getMonth()); // returns 7
For your first example - read MDN docs for Date.getMonth(). It returns a zero-indexed numerical month representation where January is represented by 0, and December by 11. Thus, July is represented (correctly) by 6.
The second one doesn't use a format per se, it's passing the values as parameters to the Date constructor. The MDN docs for Date() state that the parameter you're passing with a value 7 is also a monthIndex. Thus, to correctly notate July, pass 6 instead.
The getMonth method returns a zero based value. Meaning 0 indicates the first month. Creating a new Date object like in your second example follows the same principle.
See these examples.
The months in a Date object are zero indexed. That means they range from 0 to 11.
new Date('July 20, 69 00:20:18') creates a Date with the month of July which has the index 6.
new Date(1980, 7, 20) creates a date with the month with the 7th index, which is August.
Here you can find more details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
That diference happens because when using integer parameters to new Date(1980, 6, 20)
the month parameter starts in 0, not 1. example:
0 - january
1 - february
...
5 - july
6 - august
...
11 - december
So your result is
var dateString = new Date('July 20, 69 00:20:18') // "1969-07-20T03:20:18.000Z"
var dateInt = new Date(1980, 7, 20) // "1980-08-20T03:00:00.000Z"
console.log(dateString.getMonth()); // 6
console.log(dateInt.getMonth()); // 7

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 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