Javascript date parse error? [duplicate] - javascript

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.

Related

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

How to get only date part (dd/mm/yyyy) of a date object without time in JavaScript? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I am trying to create date object without time info like 23/09/2019.
I already tried many solutions like;
new Date(d.setHours(0,0,0,0))
new Date(dateString).toUTCString().split(' ').slice(0, 4).join(' ')
new Date(d.getFullYear(), d.getMonth(), d.getDate())
new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate())
However, all of this either give me string or date with time Tue Aug 27 2019 00:00:00 GMT+0300
I want to get this 23/09/2019 as a date object. When I write typeof result it should return date.
I know js Date object stores date and time but
Is it possible to get something like this?
Have a look at Date - Javascript | MDN for Date methods to see more details. You can compose the date components however you like.
If you want the date/month/year, you just need to call those methods:
function zeroPad(n) {
return n < 10 ? '0'+n : n;
}
var d = new Date();
var s = zeroPad(d.getDate()) + '/' + zeroPad(d.getMonth()+1) + '/' + d.getFullYear();
console.log(s);
See also getUTCDate, getUTCMonth, getUTCFullYear for UTC.

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

JavaScript: get first and last day of month with format YYYY.MM.DD [duplicate]

This question already has answers here:
Get String in YYYYMMDD format from JS date object?
(53 answers)
Closed 6 years ago.
I'm trying to get the first and last day of the month with the format 2016.02.29, but I'm not sure how to turn the date into this format.
The following code (taken from here):
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
Gives this format: Mon Feb 29 2016 00:00:00 GMT+0100 (W. Europe Standard Time).
How do i make it look like 2016.02.29? I could manipulate the string to get the result I want, but isn't there a way to get it by just defining a format?
You can use the MomentJs to format any date by specifying the exact format.
Load the momentjs or momentjs + locale script in your page
moment().format('YYYY.MM.DD');

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