getYear() and setYear()
getYear() and setYear() are legacy Date methods with awkward year handling rules. Modern code should use getFullYear() and setFullYear() instead.
Overview
getYear() and setYear() are legacy Date methods with awkward year handling rules. Modern code should use getFullYear() and setFullYear() instead.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
javascript.builtins.Date.getYear Deprecated | 1 | 12 | 1 | 1 | 18 | 1 |
| Built-in object | ||||||
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)
Syntax
JAVASCRIPT
// getYear() and setYear() example
// See MDN Web Docs for details 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.
Related links
Powered by web-features