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

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.

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

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.

JavaScript - Date.now() returning incorrect date [duplicate]

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 4 years ago.
I want to get the amount of days between a set date and today.
I can't figure out why I keep getting wrong results when I get today's date dynamically instead of hard-coding it.
HTML
<span id="today1">xxx</span> days - wrong
<br>
<span id="today2">xxx</span> days - wrong
<br>
<span id="hardcoded">xxx</span> days - correct
JS
var startDate = new Date(2016,04,01).getTime();
var todayDate1 = new Date().getTime();
var todayDate2 = Date.now();
var hardcodedDate = new Date(2018,04,08).getTime();
$("#today1").html(Math.floor((todayDate1 - startDate)/8.64e7));
$("#today2").html(Math.floor((todayDate2 - startDate)/8.64e7));
$("#hardcoded").html(Math.floor((hardcodedDate - startDate)/8.64e7));
Result
707 days - wrong
707 days - wrong
737 days - correct
JSFiddle
What am I missing?
Today is April 8th, 2018. new Date(2018, 4, 8) is May 8th, 2018. Quoting the MDN documentation on the Date constructor:
The argument month is 0-based. This means that January = 0 and December = 11.
Use 3 as the month parameter to refer to April.

javascript date() object for 8 days in the future [duplicate]

This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 8 years ago.
I'm trying to figure out the proper way to get the javascript Date object for 8 days in the future from today.
Why? because I want to subtract a given Date from today's date and if it is less than or equal to 8 days in the future then do something.
So, I figured out how to parse the given date with new Date('2014-11-21T00:00:00.000-05:00') and get the date object from that and then I can subtract it from today like so: new Date('2014-12-25T00:00:00.000-05:00') - new Date() and then I have to compare that to the date object for 8 days in the future - today's date object.
Here's how I thought about doing the date object for the date 8 days in the future: I created a new Date with all measurements of time the same as today except I added 8 to the day. This works, except what if today is less than 8 days from the end of the month, then how do I get it to overflow into the next month. For example the 27th + 8 ( of october ) should be november 4th and not october's 35th.
var date = new Date();
date.setDate((date.getDate() + 8));
//date.setDate((date.getDate() - 8)); example of subtracting
This is how you add or subtract days from a date. This will automatically account for month to month role over.

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