The code below shows the day, month and year as:
6
3
116
Which is obviously wrong dates.
var date= new Date();
var day=date.getDay();
var month=date.getMonth();
var year=date.getYear();
console.log( day );
console.log( month );
console.log( year );
function next() {
};
Fiddle.
getDay() returns the day of the week from 0-6 for Sun-Sat,
getMonth() returns the month from 0-11, so 3 means April,
getYear() has been deprecated and replaced with getFullYear() which should be used instead.
It just looks like all of these functions do something different than what you were expecting.
To get day of the month from 1-31: getDate(),
To get month like you were expecting, just add 1: getMonth() + 1
You're querying the wrong functions and misunderstanding the output.
getDay() returns the day of the week.
getMonth() returns the month, but January starts with 0.
getYear() returns the year minus 1900
You are probably looking for:
getDate(), getMonth()+1, getFullYear()
Encoding in javascript for months from is 0-11 (not 1-12).
For year, you could use getFullYear() instead of getYear()
"Date.prototype.getFullYear()
Returns the year (4 digits for 4-digit years) of the specified date according to local time."
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Related
This question already has answers here:
`date.setMonth` causes the month to be set too high if `date` is at the end of the month
(8 answers)
Closed 3 years ago.
I found it when i set month
Today is 08-31
If i increase month, then today will be 09-30
But it return 10-01
If i decrease month, then it returns 09-01
date = new Date()
console.log( date )
// Fri Aug 31 2018 11: 28: 47 GMT + 0900(한국 표준시)
console.log( date.getMonth() )
// 7
console.log( date.setMonth(date.getMonth() + 1) )
// 1538360927857
console.log( date.toISOString() )
// "2018-10-01T02:28:47.857Z"
console.log( date.setMonth(date.getMonth() - 1) )
//1535768927857
console.log( date.toISOString() )
//"2018-09-01T02:28:47.857Z"
Can anyone explain me why it happens?
When the current date is 08-31, it is not entirely clear what is should mean to set the month to 9 because 09-31 is an invalid date.
That's why at some time someone had to define what setMonth should do in such situations. It seems that the defined behavior differs from the one you expect (you expected the date to become 09-30).
MDN (for setMonth) has some information about this:
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.
It also refers to the actual specification which gives the exact algorithm which is used to calculate the result.
The reasons for the specification being as it is now are manifold, usually simplicity of the algorithm as well as the behavior on non-edge-cases are important factors.
A good explanation can be find on 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.
In your case setting setMonth(getMonth()+1) will return October 1st because September has only 30 days. Then, when you tries to decrease month to one you back to September 1st.
Javascript Date object - monthIndex is Zero (0) based, just like arrays(zero indexed).
MDN
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
Hi I have following code that is suppose to extract day,month and year part separately.
$scope.$watch('inlineDatepicker.date', function() {
alert('hey, myVar has changed!' +$scope.inlineDatepicker.date);
alert($scope.inlineDatepicker.date.getDay());
alert($scope.inlineDatepicker.date.getMonth());
alert($scope.inlineDatepicker.date.getUTCFullYear());
});
Problem with the code is I can extract year correctly but day and month do not extract correctly. I tried as well
alert($scope.inlineDatepicker.date.getUTCDady());
alert($scope.inlineDatepicker.date.getUTCMonth())
Still wrong day and month.
Please let me know how I can change it to get correct month and day values. Thanks. Here is the plunker for it.
http://plnkr.co/edit/8v75gsz8ODUrTfu8S0sh?p=preview
To get day of month, use getUTCDate()
Month is zero based, so 0 means January, 11 December
Sample
$scope.inlineDatepicker.date.getUTCDate(); //prints day of month
$scope.inlineDatepicker.date.getUTCMonth() + 1; //prints month, 0 based, so add 1
$scope.inlineDatepicker.date.getUTCFullYear(); //prints 2015
Get day returns the day of the week. Use getDate() to return the day of the month. If you do getMonth(), january is 0 and december is 11.
You can see the reference documents here: http://www.w3schools.com/jsref/jsref_obj_date.asp
I want to count down the days until a particular event using momentjs but I'm getting an unexpected result.
With today's date being 17th April, and the event date being 14th May, I want the resulting number of days to be 27, however my code gives me a result of 57. What's wrong?
function daysRemaining() {
var eventdate = moment([2015, 5, 14]);
var todaysdate = moment();
return eventdate.diff(todaysdate, 'days');
}
alert(daysRemaining());
When creating a moment object using an array, you have to take note that months, hours, minutes, seconds, and milliseconds are all zero indexed. Years and days of the month are 1 indexed. This is to mirror the native Date parameters.
Reference
So either change the month to 4 to reflect May or parse the date as an ISO 8601 string
function daysRemaining() {
var eventdate = moment("2015-05-14");
var todaysdate = moment();
return eventdate.diff(todaysdate, 'days');
}
alert(daysRemaining());
Just to add for anyone else that comes across this - there's actually a helper that does the phrasing etc for you:
https://momentjs.com/docs/#/displaying/to/
/* Retrieve a string describing the time from now to the provided date */
daysUntil: function(dateToCheckAgainst){
return new moment().to(moment(dateToCheckAgainst));
}
// Sample outputs
"in three months"
"in two months"
"in 25 days"
That's because months are zero indexed. So 5 is actually June ;)
I am having trouble understanding why a Date in Javascript come out wrong. For example...
$scope.campaign.date_start = new Date(19/11/2014);
Wed Nov 19 2014 00:00:00 GMT+0000 (GMT) - correct
$scope.campaign.date_start.getDay() = 19 - correct
So far so good, however getting the Month and the year gives me incorrect values....
$scope.campaign.date_start.getMonth() = 10 - incorrect should be 11
$scope.campaign.date_start.getYear() = 114 incorrect should be 2014
What I'm I going wrong here?
JavaScript getMonth() will always return actual month-1. For the year, you will need to use getFullYear() to get the 4 digit year
getMonth is 0 based, so you need to +1 to get the number of the current month.
getYear is deprecated, and may break at some point in future, it (generally) returns the number of years since 1900. The correct method to use is getFullYear
Date.getMonth() is 0-base (January is month 0).
Date.getYear() is deprecated and is years since 1900 (hence 114).
http://www.w3schools.com/jsref/jsref_obj_date.asp
getMonth() is zero-based. So you will need getMonth()+1
getYear() was not Y2K compliant, so a new function getFullYear() was created to return 4-digit years.