I have an object with a date property. I set the date to 01 April 2000 and can see in the debugger that it is set properly to the same date. However when I do a getMonth() on the same date object it returns month as 3 ( March ) . Why is this happening. Does it have anything to do with UTC or Localization both of which i am not using ?
You need +1 for getMonth function
var month = date.getMonth() + 1
this is normal behavior. Months starts with Zero(i.e january is 0). thats why its giving 3 for april. use adding 1 with month to get exact month's digit value.
i guess, to help indexing javascript starts month with zero.
suppose an array of months in string form then we dont have to worry to fetch correct month from string.
Related
here is code:
date: new Date(2015,12,8)
and html:
<span>{{task.date | date:'MM/dd/yyyy'}}</span>
but angularJS displays this date like
01/08/2016
and I don't know why it's display +1 month
what I missed?
In JavaScript Date object the months are indexed starting with zero - i.e. Januray == 0.
In other words, you may want to change your date creation as follows:
date: new Date(2015,11,8)
please, notice that 11 stands for December
Months are indexed from 0. So your january month is 0 and Feb is 1 and so on. The reason why it starts with zero and not one, may be because it helps with indexing into arrays.
I'm trying to validate a uk date using this code:
function ukdate(d) {
var p = new Date(d.split('/')[2], d.split('/')[1] -1, d.split('/')[0]);
if(p.toString() !== 'Invalid Date') {
return p;
}
}
http://jsfiddle.net/GE3xU/1/
so if I try ukdate('31/12/1981') it correctly returns "The Dec 31 1981". However if i try ukdate('12/31/1981') it returns "Tue Jul 12 1983".
Why is this happening? I'm expecting the second test to return invalid date because 31 is not a valid month.
JavaScript is converting your date for you.
In simple examples, you can get the last day of a given month by asking for the 0th day of the following month. Similarly, the "32nd of August" would be corrected to the 1st of September.
Months work similarly. The 13th month of a given year is the 1st month of the next. The 0th month of a year is December of the previous.
31 % 12 = 7, hence July, and floor(31/12) = 2 hence the year being shifted forward by two.
This is intended behaviour for JavaScript.
May I interest you in <input type="date" />? It uses whatever format is defined on the user's computer (ie. it is "locale-aware"), which is already excellent for user experience. On top of that, supporting browsers will render a calendar date picker, especially useful on phones too. Internally, the date is in "standard" YYYY-mm-dd format.
The month value is divided by 12 and added to the year, then the remainder is used as the actual month value.
See the spec
Let ym be y + floor(m /12).
Let mn be m modulo 12.
I'm working on a jQuery credit card expiration date validation script. Credit cards expire after the last day of the expiration month. For instance, if the card expires on 8/2013 then it's good through 8/31/2013.
In the past on the server side I've determined the last day of the month by adding 1 to the current month, then subtracting 1 day.
Today I noticed that when creating a new date, if 0 is applied to the 3rd parameter of the JavaScript Date() object, the resulting date will be the end-of-month day. But I've been unable to locate any online documentation to affirm this observation.
Here is some sample code.
var month = 10;
var year = 2013;
var expires = new Date(year, month, 0);
alert(expires);
And here is a jsFiddle example that I created.
This is a bit confusing, because I thought in JavaScript months were zero based. I've tested this in Chrome, Firefox, IE, and Safari, and the behavior appears consistent. The returned date consistently displays the last day of the month. This looks like a lucky find, but I'd really like to understand what is happening here.
Am I safe to run with this approach to assigning an end of month date, and if so is there some online documentation that I can point to which affirms this? Thanks.
Months are zero-based. That creates an end-of-month date in the previous month. Month 10 is November, so creating a date with day 0 in November gives you the end of October (month 9).
That is, day 0 in November means "the day before 1 November", which is the last day of October. Day -1 in November would be 30 October.
I have a date object:
var thedate = new Date("2012-05-02T11:00:00.000+0000");
When I do getMonth() I get 4, but when I do getDay() I get 3? I want to make it so that when I call getDay, I get what’s reflected in the original string (2). I could just subtract 1 from getDay(), but I’m not sure if that’s the right way to do it, and whether it applies to all dates.
According to MDN, getMonth will return a number in the range 0-11 (so 0 is for January), and getDay will return the day of the week in the range 0-6 (so 0 is for Sunday). If you want to get the day in month, you should use getDate, which will return a number in the range 1-31.
getDay/getMonth will return the index of the day, which starts from 0, therefore +1.
getDay() Returns the day of the week (from 0-6)
Read here: http://www.w3schools.com/jsref/jsref_obj_date.asp
I'm am trying to format a date in Javascript but the date command is returning the wrong date unless I use toUTCString() which returns the correct date, I've tried different ways of giving the date to the Date() function and both get and getUTC functions to get the date. I've also tried on different browsers (Chrome, Safari, FireFox) and what makes in even more confusing is if I do it in Chrome's inspector is works perfectly. And I missing something obvious?
var d = new Date(1324141200000);
// return "Sat, 17 Dec 2011 17:00:00 GMT" - Correct!
alert(d.toUTCString());
// returns "6-11-2011" - Wrong!
alert(d.getUTCDay() +'-'+ d.getUTCMonth() +'-'+ d.getUTCFullYear());
The "getUTCDay()" function returns the day of the week. The months are numbered from zero. Saturday is the sixth day of the week (in JavaScript land at least), and 11 is the 12th month counting from zero.
Thus, all is well.
The day of the month can be retrieved with "d.getUTCDate()".
d.getUTCDay() // day of week
d.getUTCMonth() // zero based index
Instead of getUTCDay, you want getUTCDate. And getUTCMonth returns 0-11 (0 = January). Section 15.9.1 of the specification may help, but the language is heavy-going.
Use
getFullYear()
function to get the year,
getMonth()
function to get the month, and
getDate()
function to get the day.