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
Related
I'm retrieving a list of dates from my database and getting the month from those dates.
When I get month for this date it returns 0 for January.
var date = new Date('2015-12-31T22:57:12.000Z').getMonth();
When I get month for a similar date it returns 11 for December.
var date2 = new Date('2015-12-31T12:24:29.000Z').getMonth();
This will give the right result:
var date = new Date('2015-12-31T22:57:12.000Z').getUTCMonth();
If your timezone is GMT+02, then you can try like this also:
var date = new Date('2015-12-31T22:57:12.000+0200').getMonth();
As you want to why this both Dec string dates returning different month? This is the answer:
From your profile i could see your are from south Africa which is GMT+2 timezone.
First date string : 2015-12-31T22:57:12.000Z, which is 31 Dec and 22.57 hours mid night. While creating date object out of string date at client browser side, It's takes your local timezone into account. Its GMT + 2.
Thus, it adds 2 hours: 2015-12-31T22:57:12.000Z + 2 hours => 22.57 hours shift to next day after addition of 2 hours(22.57 +2 => next day 00.57), which would be January month morning 0.57 hours. So it returns 0 (January) Month.
While second : 2015-12-31T12:24:29.000Z which is 12:24 hours which shifts to 14.24 in same day after addition on 2 hours as above. So you are still in 31 Dec. hence you receive December for this.
The months in javascript are numbered from 0 to 11, days from 1 to 31
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 need function for getting the start of the same week last year.
For example I have date : 2016-01-10 and it's the first date of the 3d week in 2016, so I need the first day of the 3d week of 2015 and it would be 2015-01-11
(I always need the first day and let's consider that I allways get the first dat of week as param for my function).
I have created such function:
var getTheSameWeekLastYear = function (date) {
var date = moment(date).startOf("week"),
weekNo = date.week();
// move a year ago and set the same week
return date.add(-1, "y").week(weekNo).startOf("week");
}
And it works fine until I got week wich starts in last year and end this year. For example, week#1 2016 - starts 2015-12-27 and ends 2016-01-02.
I this case I am getting thee same week for a year before last year - 2013-12-29.
JSFiddle
What is the best way to solve this problem?
Probably momentjs has any built in function for my task?
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
}
How could I get from moment JS the week number from a date in the past only from a moment formatted object from a day selected?
$(document).ready(function(){
var weeknumber = moment("12-25-1995", "MM-DD-YYYY").week();
console.log(weeknumber);
});
According momentjs docs:
Because different locales define week of year numbering differently,
Moment.js added moment#week to get/set the localized week of the year.
The week of the year varies depending on which day is the first day of
the week (Sunday, Monday, etc), and which week is the first week of
the year.
For example, in the United States, Sunday is the first day of the
week. The week with January 1st in it is the first week of the year.
So, if you are having problems getting the right week number use .isoWeek()
$(document).ready(function(){
var weeknumber = moment("11-26-2016", "MMDDYYYY").isoWeek();
alert(weeknumber);
});
Example
You can also use format()
Examples:
moment().format('w') // as .week() like '1'
moment().format('W') // as .isoWeek() like '1'
moment().format('ww') // as .week() (2 digits) like '01'
moment().format('WW') // as .isoWeek() (2 digits) like '01'
ISO Week date: https://en.wikipedia.org/wiki/ISO_week_date
More info: https://momentjs.com/docs/#week-year-week-and-weekday-tokens
to get week number by current date
moment(moment().toDate(), "MM-DD-YYYY").isoWeek()
This is not relevant to the question but if you want to get 1 for Monday, 2 for Tuesday, etc
moment().format("d")