I can't get Month and Day in Date Javascript [duplicate] - javascript

This question already has answers here:
Javascript returns wrong Date values (NodeJS)
(3 answers)
Closed 4 years ago.
I am try get month and day but value is different o my text:
var fech = new Date("2021-02-28T00:00:00");
document.writeln("Year=" + fech.getFullYear());
document.writeln("Month=" + fech.getMonth());
document.writeln("Day=" + fech.getDay());
Result is:
Year=2021 Month=1 Day=0
https://jsfiddle.net/j0qn2jjr/3/

getDay() returns the day of the week (Sunday is 0, Monday is 1, etc). What you're probably looking for is getDate().
Also, getMonth() returns 0-11 (January is 0, February is 1, etc).
Read up here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

GetMonth() returns the number month, which in this instance in Feb, using a 0 index, Feb would actually be month 1 in this scenario. Use getMonth()+1 if you need to get the numbers.
#Máté Safranka has explained about the day problem too!

Related

Setting a lower month on a date with d.getDate() === 31 returns that same month with date set to 1 in JavaScript [duplicate]

This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 2 years ago.
Code here:
const d = new Date(2020, 9, 31);
d.setMonth(8);
console.log(d);
// Result: Thursday October 1st (pardon can't type the whole thing, but you get what my problem is).
I expected September, pls help
According to 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.
So since your date has '31st' as day, after setting the month to September, it calculates '31' days hence you get the same month.

Why does my JavaScript date show wrong day of the month? [duplicate]

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Date.getDay() javascript returns wrong day
(8 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
So I just started programming (I don't know if this is a correct word for it). I started making a website and wanted to add a date and clock. I followed a YouTube tutorial and it works but the month number is one month behind. Is there anyway to fix this problem? Also I wanted to make month number double if its number is less than 10. I wrote the JavaScript code and it messed with the month name (In this case Gegužė).
The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
const moonLanding = new Date('July 20, 69 00:20:18');
console.log(moonLanding.getMonth()); // (January gives 0)
// expected output: 6

get current month prints previos month [duplicate]

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 2 years ago.
I want to get the current month. we are in may now but a weird thing happens:
var thisDate = new Date();
console.log(thisDate);
console.log(thisDate.getMonth());
Output:
Thu May 14 2020 12:25:19
4
The expected value is 5
getMonth() return number from 0 to 11, so just add +1 to that.
The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth
Use this code
var thisDate = new Date();
console.log(thisDate);
console.log(thisDate.getMonth() + 1);

Strange behaviour of Date.getMonth() - Month difference [duplicate]

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 5 years ago.
In a project I need to convert in javascript a timestamp into a human readable string, however the number of month was incorrect so I performed the following test in a javascript console :
How to explain why there is a one month difference ?
Thank you in advance
Javascript dates start at 0 for months
example:
0: Jan
1: Feb
2: Mar
etc
In javascript months start from 0. So January is 0, and december is 11
docs
Return value An integer number, between 0 and 11, representing the
month in the given date according to local time. 0 corresponds to
January, 1 to February, and so on.
Javascript's date.getMonth starts the month count from 0. So you need to add +1.
var date = new Date();
console.log(date.getMonth() + 1);

Javascript Date shows the wrong value when using alert [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript is creating date wrong month
In my code, I create Date by this line:
var date = new Date('2012', '01', '20')
and then using:
alert(date);
I get the result:
I don't really understand why not Jan instead of Feb?
jsFiddle: http://jsfiddle.net/U5m8N/
The "Month" part of var date = new Date('2012', '01', '20'); is zero-indexed.
Start counting at 0, and you got your month. (So, January is 0, Feb 1, etc.)
Also, while JavaScript does accept strings as parameters, you should be using integers, as the documentation suggests:
Date Documentation
Javascript Date Object's Month is 0 indexed. http://www.w3schools.com/jsref/jsref_obj_date.asp. put 0 instead of 1 for Jan

Categories

Resources