Is the result of Date.prototype.getYear() a feature or a bug? | SoloLearn: Learn to code for FREE!

+1

Is the result of Date.prototype.getYear() a feature or a bug?

When entering a date string as a parameter on the Date object the result that we get from applying the getYear() to this object is interesting. For dates of format YY in the range from 50 to 99 it appears to work fine. But when entering 0 to 49 the getYear() method shows three digit number instead of two. Is this a bug or should leave it at that because these are the limitations of this method? Here is a sample code: const moonLanding = new Date('July 20, 99 00:20:18'); console.log(moonLanding.getYear());// shows 99 const moonLanding = new Date('July 20, 20 00:20:18'); console.log(moonLanding.getYear());// shows 120

12/30/2019 12:53:19 PM

Erialdo Meta 🇦🇱

4 Answers

New Answer

+2

According to the w3schools reference below, `getYear` method is deprecated and developers are encouraged to use `getFullYear` method instead. As I understand it, a deprecated method is kept for backward compatibility reasons, but there is a chance that it will be removed in the future. And it is possible that the team no longer maintain the method, which probably explains the reason for the unexpected behaviour of `getYear`. https://www.w3schools.com/jsref/jsref_obj_date.asp

+2

Ipang thanks for the info. Looked up in the internet for this but have not looked at w3schools.

+2

I should have payed more attention then.

+1

You're welcome, just stumbled on it from Google anyways 👌