35

I was working with JavaScript's Date object, and somewhere for the first time I needed to extract the year part of the date. What I saw was a weird behavior. It returns 113 instead of 2013 and I can't figure out how these two numbers might be related, except that the last two numbers might be the same.

var date = new Date();
console.log(date.getYear()); // prints 113
console.log(date.getFullYear()); // prints 2013

How these functions are different? And why having a function to getFullYear? It just makes no sense to me to get part of a year.

James Allardice
  • 164,175
  • 21
  • 332
  • 312

3 Answers3

60

It just makes no sense to me to get part of a year.

Back in the day, when memory was expensive and the Year 2000 was far in the future, it did make sense for people to represent 1975 as 75. In retrospect a short-sighted decision.

date.getFullYear() == date.getYear() + 1900
Thilo
  • 257,207
  • 101
  • 511
  • 656
8

getYear() returns year minus 1900. This has been deprecated for a while now, it's best to use getFullYear().

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
SpaceCowboy
  • 543
  • 6
  • 16
3

Its a Y2K thing, basically getYear method returns the year minus 1900.

so, I encourage to move over to getFullYear & use that instead.

CuriousMind
  • 33,537
  • 28
  • 98
  • 137