get current month prints previos month [duplicate] - javascript

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

Related

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

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!

Javascript date parse error? [duplicate]

This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 5 years ago.
let date = new Date("2017-09-12T12:00:00");
console.log(date.getUTCMonth());
Here I am expecting it would log 09 for a month but it's logging 08.
Year, day , hour and minute gets parsed correctly though. what's going on here? How can I extract 09 from the above date string?
The getUTCMonth() is a zero-based value — zero is January.
For more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMonth
date.getUTCMonth() method returns the month (from 0 to 11) for the specified date.
So to get what you expect, you should add 1.
Months are 0-indexed in Javascript.
var date = new Date(date),
month = '' + (date.getMonth() + 1),
day = '' + date.getDate(),
year = date.getFullYear();
For a quick example is how you would format it if you wanted to format it in a YYYY - MM - DD format.

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

Add 30 days to the time now [duplicate]

This question already has answers here:
Add 30 days to date (mm/dd/yy)
(4 answers)
Closed 6 years ago.
I'm using Date.now() to get the current date.
I then need to add 30 days to this.
How can I do this?
Would I just work out how many seconds in 30 days, then add this on?
var d = new Date();
console.log(d);
d.setDate(d.getDate() + 30);
console.log(d);
Date.prototype.getDate returns current date day number.
Date.prototype.setDate set date number of given date to value passed to it.
If value exceeds normal date range of month date extra value will handle needed math and change month (or year if necessary) and shows correct result.
var date = new Date;
date.setDate(date.getDate() + 30);
console.log(new Date, date);

Incorrect Date() outcome [duplicate]

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 9 years ago.
I'm using the javascript Date() function to work with dates.
I have the following:
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
console.log(y);
console.log(m);
console.log(d);
This returns, in the console:
2013
9
1
Which correct me If I'm wrong, is 1 month behind the actual date of today.
I'm in the UK, so could BST be affecting the outcome of this?
Thanks
var m = date.getMonth();
Because month begins at 0.
0-January
1-February
2-March
3-April
4-May
5-June
6-July
7-August
8-September
9-October
10-November
11-December
JavaScript months are 0 based.
Date.getMonth
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).
Just to be confusing!
So always remember to add 1 to the result of date.getMonth() if you want the actual month number.
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
Source: w3schools
So, you are getting 1 month less than the actual month of your local time.
You can use:
console.log(("0" + (m+1)).slice(-2))
JavaScript month start from 0.

Categories

Resources