getYear() and setYear()
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
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 | |
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 |
Syntax
// getYear() and setYear() example
// See MDN Web Docs for details Live demo
Compare getYear and getFullYear
See the legacy year offset returned by getYear.
Compare setYear and setFullYear
Update the same date with the legacy and modern setters.
Wrap the legacy result
Convert the legacy year value into a full year for display.
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.