This question already has answers here:
Date.getDay() javascript returns wrong day
(8 answers)
Closed 3 years ago.
I'm having trouble with new Date constructor,
when i set new Date(date) and ask console to print it, then it returns the date i want but if i try to get the day/month it returns my current day/month as if i was setting to new Date().getDay()
let date, end;
date = new Date()
end = new Date(date.getFullYear(), date.getMonth() + 1, 0)
console.log(end) // prints: Thu Oct 31 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)
console.log(end.getDay()) // prints: 4
I want the output to be 31 but it's not working, am i doing something wrong?
if you want to print the date (1-31) , you have to use the method getDate().
let date, end;
date = new Date()
end = new Date(date.getFullYear(), date.getMonth() + 1, 0)
console.log(end) // prints: Thu Oct 31 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)
console.log(end.getDate()) // prints: 31
I think you are using the wrong method
let end = new Date(date.getFullYear(), date.getMonth() + 1, 0)
console.log(end) // prints: Thu Oct 31 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)
console.log(end.getDay()) // Returns the day of the week
console.log(end.getDate()) // Returns the day of the month
getDay() doesn't return the number of the day in the month. It returns the day in the week.
Use getDate() for your desired return. Also you can check the document in here
Related
This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 2 years ago.
This is driving me crazy!
console.log(new Date(2020, 11, 4));
A simple date, 4th of November 2020.
Output:
Fri Dec 04 2020 00:00:00 GMT+0000 (Greenwich Mean Time)
WHAT!
So lets try an alternative:
const now = new Date()
console.log(now.getFullYear(), now.getMonth(), now.getDate());
Outputs:
Thu Oct 29 2020 10:12:56 GMT+0000 (Greenwich Mean Time)
Lets confirm some things now:
typeof now.getFullYear();
"number"
typeof now.getMonth();
"number"
typeof now.getDate();
"number"
typeof 2020
"number"
typeof 11
"number"
typeof 4
"number"
What is going on?
Check out the Date constructor:
monthIndex
Integer value representing the month, beginning with 0 for January to 11 for December.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
So new Date(2020, 0, 1) is 01.01.2020, and your test case new Date(2020, 11, 4) is not Nov 4th, but Dec 4th.
So I'm trying to get add 1 day to today's date (tomorrow), but instead of having it it exactly 24 hours from when the query is run, I want it to be basically tomorrow morning 00:00..
eg if it's currently Tue Jun 23 2020 13:01:57 GMT+0200 when I run new Date(), I need to get Wed Jun 24 2020 00:00:00 GMT+0200 to post to server (using axios).
Here's what I've tried, but it returns todays date at midnight.
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(new Date().getDate() + 1);
let payload = {
available_from: tomorrow.setHours(0,0,0,0),
}
axios.post(url, payload)......
Any ideas?
You can just use Date.setHours to set the time on today's date to 24:00:00.000 i.e. tomorrow morning at midnight.
const tomorrow = new Date();
console.log(tomorrow.toString());
tomorrow.setHours(24, 0, 0, 0);
console.log(tomorrow.toString());
I need to increment hours to a date, but I can't get the correct date after change the hours. For example:
Change the current date
let x = new Date(); // 2018-05-30T00:17:04.888Z
x.setHours(x.getHours() + 24); // 2018-05-31T00:17:04.888Z
Great! Now the date should be 2018-05-31, right?
But if I try to do the following:
x.getDate();
It still returns old date: 2018-05-30 and the same happens for x.getHours()
Is there a way to handle that?
It seems to work just fine.
var date = new Date()
date
>Wed May 30 2018 03:26:19 GMT+0000 (UTC)
date.setHours( (date.getHours() + 23) )
>1527726379425
date
>Thu May 31 2018 00:26:19 GMT+0000 (UTC)
date.getDate()
>31
I have this code:
var nextDate = new Date("2016 01 31");
nextDate.setMonth(nextDate.getMonth() + 1);
I'm expecting the result to be Feb 28 2016, but it shows Mar 02 2016 instead.
Why? Is there any solution for it?
There is only 29 day in February, therefore, February 31 February will translate to Mars 2.
You need to update the days in your date object to the last day of that month. You can get the last day of the month by specifying a function that sets the date to 0:
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
This is because February has 29 days, and when you set new month from January, which has 31 day, to February then the difference of the days are transferred to another month.
Easy way to do it is just create new Date instance.
You might need to implement some logic to get corresponding dates right
Possible work around with a helper function: after setMonth, check if the results doesn't contain a month equal to the expected month and if so, use setDate(0), which sets the date to last day of the previous month. e.g.
Date.prototype.addMonths = function(months){
var m = this.getMonth() + (months || 1);
this.setMonth(m);
if(this.getMonth() !== (m % 11)) //11: month is 0 based
this.setDate(0);
}
var nextDate = new Date("2016 01 31");
nextDate.addMonths(1);
document.writeln(nextDate);
months || 1 only is meant to have a default value if no month was submitted. m % 11 is needed in case of year transitions. 11 and not 12 because javascripts month (and thus getMonth) is 0 based.
Why do these two dates are differents :
var date1 = new Date();
date1.setFullYear(2012); // year (four digits)
date1.setMonth(10); // month (from 0-11)
date1.setDate(1); // day of the month (from 1-31)
var date2 = new Date(2012, 10, 1, 0, 0, 0, 0);
Result :
Date 1 : Sat Dec 01 2012 14:56:16 GMT+0100
Date 2 : Thu Nov 01 2012 00:00:00 GMT+0100
whereas these two dates are equals :
var date3 = new Date();
date3.setFullYear(2012); // year (four digits)
date3.setMonth(9); // month (from 0-11)
date3.setDate(1); // day of the month (from 1-31)
var date4 = new Date(2012, 9, 1, 0, 0, 0, 0);
Result :
Date 3 : Mon Oct 01 2012 14:56:16 GMT+0200
Date 4 : Mon Oct 01 2012 00:00:00 GMT+0200
Another question is why do date1.setMonth(10) gives a date in December (should be November).
Finally got it. new Date() sets the date to the current date and time. In other words, October 31st (at the time of this writing).
When you then try to set the month to November, what's it to do? November only has 30 days... so it wraps it round to December.
If you change the order so that you set the day-of-month before the month, it works:
var date1 = new Date();
date1.setFullYear(2012); // year (four digits)
date1.setDate(1); // day of the month (from 1-31)
date1.setMonth(10); // month (from 0-11)
Or as implied by jbabey's answer:
var date1 = new Date();
date1.setFullYear(2012); // year (four digits)
date1.setMonth(10, 1); // month (from 0-11) and day (1-31)
The documentation isn't terribly clear, but it's at least suggestive:
If a parameter you specify is outside of the expected range, setMonth attempts to update the date information in the Date object accordingly. For example, if you use 15 for monthValue, the year will be incremented by 1 (year + 1), and 3 will be used for month.
("Accordingly" is far from precise, but it means the implementation is at least arguably correct...)
setMonth accepts a second parameter:
If you do not specify the dayValue parameter, the value returned from the getDate method is used.
When you set the month to 10 (November), it grabs the current day value (31) and sets that as the day. Since there are only 30 days in November, it rolls you over to December 1st.
You're creating a var containing the current date (new Date()) and then you're changing some of it's keys (year, month and day).
On the other hand new Date(2012, 10, 1, 0, 0, 0, 0) means "create a date object with those exact values".
And that's why your date objects aren't equal.