2

I'm running the next simple JS code in chrome console:

var d = new Date();
console.log(d.__proto__);
console.log(d.getYear());
console.log(d.getMonth());
console.log(d.getDay());

And get the next strange output:

Invalid Date
115
6
5

Today is 10th July 2015. So why year, month and day are incorrect and why 'Invalid Date' for '__proto__'?

8
  • the __proto__ is the stringification of the date constructor anyway. Which is not a date Commented Jul 10, 2015 at 7:07
  • Month is right because Date months goes from 0 to 11. Jan =0, Feb=1, etc. Commented Jul 10, 2015 at 7:07
  • Note that Date.prototype is a plain Object, it is not a Date instance. Writing it to the console calls Date.prototype.toString, and since it doesn't have a time value, NaN is used so the result is an invalid date. Nice easter egg. ;-) Commented Jul 10, 2015 at 7:12
  • What would you have expected d.__proto__ to be? Commented Jul 10, 2015 at 7:14
  • @RobG: Notice they've changed that from ES5 Date.prototype. a) browsers will not yet have adopted this b) it was a rushed decision, one of the last before finalisation, and might have been a mistake. Looking for the first errate list and ES7 :-) Commented Jul 10, 2015 at 7:17

6 Answers 6

4

It all works like that because that's how the Date object is meant to work.
None of the results you mention are incorrect.

Year:

The getYear() method returns the year minus 1900;

So, for 2015, it returns 2015 - 1900, which is 115. However, getYear is deprecated, use getFullYear instead. (This does return 2015)

Month:

The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).

So, January is 0, February 1, etc.

Day:

The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday.

Basically the same story as months, they're zero-based.
If you want to get the day of the month intead, use getDate()

And finally, the prototype, __proto__ is really something you shouldn't touch. It's no more than a skeleton for a Date object, where the Date object will have it's proper values.

Sign up to request clarification or add additional context in comments.

2 Comments

…also, OP was probably looking for getDate instead of getDay
@Bergi: I literally just added that while you commented :-)
2
  • The prototype itself is an invalid date. You should not use the prototype date, since it is every date and none. Each concrete date object overrides the fields with correct values.

  • getYear gives you years since 1900. It is now 115th year since 1900, which is correct. getFullYear is arguably more useful.

  • getMonth gives you the month with January being 0. 6 means July. It, too, is correct.

  • getDay gives you the day of week, with Sunday being 0. 5 being Friday, this is correct as well. You might have wanted getDate instead.

Comments

0

d.getFullYear(), month is 0 based and d.getDay() is Day-Of-Week, also 0=Sunday based and 5 is Friday. you want d.getDate().

Comments

0

try getDate() and not getDay(), and getFullYear(). As for the month, months start from 0 (jan is 0) so july is 6 and not 7.

Comments

0

1) getYear is obsolete, use getFullYear() instead.

2) getMonth return values from 0-11, so instead of 7 (7-1) is displayed.

3) getDay returns value between 0-6

Comments

-1

Use d.getFullYear() instead of d.getYear()

var d = new Date();
console.log(d)
// Fri Jul 10 2015 12:40:09 GMT+0530 (India Standard Time)
console.log(d.getFullYear())
// 2015
console.log(d.getDay())
5 // 5 is the number of the day so it is correct as 0 represents Sunday
console.log(d.getDate())
// 10

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.