const date = new Date("2020-03-20T12:45:52.793Z");
console.log(date.getDate() + 1);
it shows 21 instead of 3
I'm new in javascript. the operation += does not works
regarding.
This has nothing to do with the operator (+, +=) used. Instead, it is to do with the function you used. From w3schools:
The getDate() method returns the day of the month (from 1 to 31) for the specified date.
See the following snippet:
const date = new Date("2020-03-20T12:45:52.793Z");
console.log(date.getDate());
As you can see, date.getDate() outputs 20, the day of the month you specified. So it is no surprise that adding 1 to this value will get you 21.
Given that you expect the output to be 3, I assume you want to get the month of the date.
To do this, you need to use getMonth and not getDate.
And as you have already correctly noticed, you need to add 1 to the month number if you want to get the result 3, because January starts at 0.
Again, the w3schools definition tells you what you need to know:
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
Note: January is 0, February is 1, and so on.
const date = new Date("2020-03-20T12:45:52.793Z");
console.log(date.getMonth() + 1);
Related
This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 1 year ago.
I try to use the date object from NodeJS v14.15.1. And when I instantiate the object with the year, month and day parameter. When I try to use the getDay() and getYear() method, I don't have the right number.
I really don't know what going on and why this method don't return what's I expected.
const date = new Date(2021, 4, 1) // I create a date object for 2021/04/01
console.log(date.getDay()) // This return 6 instead of 1
console.log(date.getYear()) // This return 121 instead if 2021
There's some unfortunate historical naming of methods here. Check out MDN's documentation for Date.
getDay returns the day of the week (Sunday = 0, Monday = 1, etc.). You probably want to use getDate instead.
Similarly, getYear returns the year minus 1900. You probably want to use getFullYear instead.
Although you didn't mention it, note that getMonth is zero-based: 0 = January, 1 = February, etc. The same is true for new Date. So new Date(2021, 4, 1) corresponds to the ISO date 2021-05-01.
I'm working on a response from a script to work out an expiry date. The response is 7200 which I've been advised from the developer is an epoch value that should equate to 3 months. I've never used Epoch before so don't understand how this works?
The formula I've been given to use is (created_at + expires_in) * 1000 which I've been advised will give me my new date.
I used dtmNow = new Date(Date.now()).toISOString(); which returned 2016-08-23T06:33:35.936Z which was correct, but when I tried dtmExpires = new Date((Date.now()+7200)*1000).toISOString(); it returned +048613-09-25T09:58:58.000Z?
I'm not sure what I'm doing wrong here?
Date.now() returns the number of milliseconds since the epoch (the current time value). If you pass a number to the Date constructor, it's used as the time value for a new Date instance. In the following:
new Date(Date.now()).toISOString()
is exactly the same as:
new Date().toISOString()
i.e. you don't need Date.now(). If you want to add 3 months to a date, use Date methods:
// Get a Date for now
var now = new Date();
// Add 3 months
now.setMonth(now.getMonth() + 3);
However, if it's currently 30 November then the above will attempt to create a date for 30 February, which will end up being 1 or 2 March depending on whether it's a in a leap year or not. So if the modified day in the month doesn't match the original, you can set it back to the last day of the previous month.
If you want to add (say) 90 days, then do that using the setDate and getDate methods similar to the following. This also takes account of daylight saving boundaries if you cross one, whereas setting the time value doesn't.
The SO console writes dates in UTC so take that into account when looking at the following results:
function add3Months(d) {
// Default to current date if d not provided
d = d || new Date();
// Remember current date
var date = d.getDate();
// Add 3 months
d.setMonth(d.getMonth() + 3);
// Set the date back to the last day of the previous
// month if date isn't the same
if (d.getDate() != date) d.setDate(0);
return d;
}
// Add 3 months to today
console.log(add3Months());
// Add 3 months to 30 November
console.log(add3Months(new Date(2016,10,30)))
I'm working on a NodeJS Projects and I get wrong Date values. And I don't get what I am doing wrong.
var d = new Date(results[1].timestamp);
console.log(results[1].timestamp); // 2016-05-10T13:29:47 <- this is right (stored at my DataBase)
console.log(d.getDate()); //10
console.log(d.getFullYear()); //2016
console.log(d.getMonth()); //4
console.log(d.getDay()); //2
console.log(d.getHours()); //15
console.log(d.getMinutes()); //29
console.log(d.getSeconds()); //47
So Month, Day and Hours are wrong.
I see these results in google chrome at my Mac
Thanks for helping
A few errors here:
getMonth returns a 0 based month. That is May is 04.
getDay returns the day of the week. I guess you want getDate
the date is parsed as UTC and getHour is according to the locale. So the hour might be different from what you want (but right here it seems to be "exact", as is it's the same value than inputted).
A tip for your next problems: Have a look at some documentation. For example the MDN.
The getDay() method returns the day of the week (from 0 to 6) for the specified date.
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
The getHours() method returns the hour (from 0 to 23) of the specified date and time.
getDay() function returns the Day of the date i.e. from sunday to saturday(0 to 6)
getMonth() function returns month frome January to December (0 to 1) so here you need to add 1 to correctly get the value
and I am afraid you misinterpreted getHours() result, because if I check for the mentioned date it gives me 13
In short: Can someone please explain to me what is going on with this function:
function daysInMonth(month, year){
return new Date(year, month, 0).getDate();
}
alert(daysInMonth(1, 2013));
What I'm really interested in understanding is, why there is a "0" after month? I just can't seem to get my head round it, I've tried to omit it, and also replacing it with "day" but both with different outcome. This function only seems to work when the "0" is passed in the Object.
Also another tricky part when calling the function, passing "0" and "1" to represent January both return the same number of days where as passing "12" and "11" to represent December return different number of days (12 returns 31 (December) and 11 returns 30 (November)).
JavaScript Date objects "fix" date settings that make no sense. Requesting a Date instance for day 0 in a month instead gives you a Date for the last day in the previous month.
Months are numbered from zero, but that function is written as if months were numbered from 1. You therefore get the same answer when you pass 0 or 1 as the month number because you're getting the number of days in the months December and January, and both of those months have 31 days.
Personally I would not have written that function that way; since it's necessary to keep in mind that months are numbered from zero in JavaScript, I'd have written the function like this:
function daysInMonth(month, year){
return new Date(year, month + 1, 0).getDate();
}
Then to get the number of days in January, you'd call it like this:
var janDays = daysInMonth(0, 2015);
The key is the JS Date object constructor. This function takes multiple parameters, but three are required: year, month, day. The day param is the day of the month, with the first day of the month being numbered as 1. The code above is really tricky. According to JS reference passing 0 for date, actually results in the last day of the PREVIOUS month. So this actually explains both the day question you have as well as the month question at the end.
See for more info: http://www.w3schools.com/jsref/jsref_setdate.asp
Passing 0 and 1 do not actually both represent January -- 1 represents january and 0 represents december of the previous year, which also has 31 days! (try running this function the year AFTER a year with a leap day, they wont have the same number of days).
Edit: Example
To get a better grasp of what is actually happening, try running this function:
function check() {
console.log(new Date(2015, 0, 0).toISOString()); // 2014-12-31T08:00:00.000Z
console.log(new Date(2015, 1, 0).toISOString()); // 2015-01-31T08:00:00.000Z
console.log(new Date(2015, 11, 0).toISOString()); // 2015-11-30T08:00:00.000Z
console.log(new Date(2015, 12, 0).toISOString()); // 2015-12-31T08:00:00.000Z
}
Looking over the previous questions and answers it appeared this should work :
var palindrome = new Date('2011-11-11');
var december = new Date('2011-11-11');
december.setDate(palindrome.getDate()+20);
//should be december, but in fact loops back over to Nov 1st)
my jsFiddle
is there a simple way to ensure that months are incremented correctly, or have I missed something obvious ?
You could do it like this:
var dayOffset = 20;
var millisecondOffset = dayOffset * 24 * 60 * 60 * 1000;
december.setTime(december.getTime() + millisecondOffset);
The getMonth() call returns a value between 0 and 11, where 0 is January and 11 is December, so 10 means November. You need to increment the value by 1 when using it in a string. If you simply output it as a string you'll see that it has the correct date. Note I also had to change the starting date format. It didn't seem to like 2011-11-11 so I made it 11/11/2011. http://jsfiddle.net/9HLSW/
Your code is correct, however you are converting it to a string wrongly.
getMonth() starts with 0 as January, and ends with 11 as December. So all you need to do is add 1 to the month like this:
alert(endDate.getFullYear() + "-" + (endDate.getMonth()+1) +"-"+ endDate.getDate());
Notice the additional brackets - cos you are performing a math operation while concatenating strings. You won't want to end up with "101" as the month.
To see whether you got the date correct, use endDate.toDateString() to display the date in a fully qualified name (i.e.: January - December).
alert(endDate.toDateString());
For more info on the Date object, check out this section in w3schools