getYear() and setYear()
The getYear() method of Date instances returns the year for this date according to local time. Because getYear() does not return full years ("year 2000 problem"), it is deprecated and has been replaced by the getFullYear() method.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
Date.getYear Deprecated | 1 | 12 | 1 | 1 | 18 | 1 |
Date.setYear Deprecated The setYear() method of Date instances sets the year for a specified date according to local time. | 1 | 12 | 1 | 1 | 18 | 1 |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Live demo
Compare getYear and getFullYear
See the legacy year offset returned by getYear.
JavaScript
Output
Press the Run button
Compare setYear and setFullYear
Update the same date with the legacy and modern setters.
JavaScript
Output
Press the Run button
Wrap the legacy result
Convert the legacy year value into a full year for display.
JavaScript
Output
Press the Run button
Use cases
-
Legacy date migration
Update older year-handling code to clearer full-year methods before bugs accumulate.
-
Historical code review
Recognize these methods when auditing older business logic or imported scripts.
Cautions
- Legacy year offsets make these methods easy to misuse and hard to read correctly.
- Date code already has enough edge cases without keeping outdated method semantics.
Accessibility
- Date bugs can surface as incorrect scheduling or stale status messages in the UI.
- Prefer explicit date handling so time-related information remains trustworthy.