To get the current 4-digit year in JavaScript, create a new Date() instance and call getFullYear() (or getUTCFullYear() when you need the year in UTC rather than the user’s local time zone). Unlike the deprecated getYear() method, getFullYear() reliably returns a full 4-digit integer such as 2026 across modern browsers and Node.js runtimes.
1. Getting the Local 4-Digit Year with new Date().getFullYear()
Instantiate new Date() for the current moment and call .getFullYear() to read the local calendar year as a number:
// Non-mutating helper returning the current local 4-digit year
function getCurrentYear(date = new Date()) {
if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
throw new TypeError("Expected a valid Date instance.");
}
return date.getFullYear();
}
// Example usage:
const currentYear = getCurrentYear();
console.log(currentYear, typeof currentYear); // e.g., 2026 "number"
- Returns a
number, not astring:getFullYear()returns2026as a number; useString(getCurrentYear())when rendering text or template literals. - Accepts an optional
Dateargument: Passing aDatemakes unit tests deterministic without altering global clock state.
2. Getting the UTC Year vs. Formatting a Specific Time Zone
Near midnight on December 31 or January 1, the local calendar year can differ from the UTC year; use getUTCFullYear() for UTC or Intl.DateTimeFormat when formatting for a specific time zone:
// Get UTC 4-digit year or format the 4-digit year for a given IANA time zone
function getYearInTimeZone(timeZone, date = new Date()) {
if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
throw new TypeError("Expected a valid Date instance.");
}
if (typeof timeZone !== "string" || timeZone.trim().length === 0) {
return date.getUTCFullYear();
}
const yearPart = new Intl.DateTimeFormat("en-US", {
timeZone,
year: "numeric"
})
.formatToParts(date)
.find((part) => part.type === "year");
return yearPart ? Number(yearPart.value) : date.getUTCFullYear();
}
// Example usage:
console.log(getYearInTimeZone(null)); // UTC year, e.g., 2026
console.log(getYearInTimeZone("Asia/Karachi")); // e.g., 2026
- Avoid time-zone drift in logs and timestamps: Backend services, databases, and shared APIs often prefer
getUTCFullYear()for consistency across regions. - Avoid manual UTC offset math: Let
getUTCFullYear()orIntl.DateTimeFormathandle daylight saving and time-zone boundaries rather than adding/subtracting hours manually.
3. Avoid Deprecated getYear() and Guard Against Invalid Dates
Never use Date.prototype.getYear(), which is deprecated and returns year - 1900 (for example, 126 in 2026), and always check Number.isNaN(date.getTime()) before reading date fields from parsed input:
// Safely extract a 4-digit year from a Date or ISO date string
function tryGetFourDigitYear(input) {
const date = input instanceof Date ? input : new Date(input);
if (Number.isNaN(date.getTime())) {
return null;
}
return date.getFullYear();
}
// Example usage:
console.log(tryGetFourDigitYear("2026-11-29")); // 2026
console.log(tryGetFourDigitYear("not-a-date")); // null
- Never use
getYear(): It returns years since 1900 and is obsolete in modern JavaScript. - Check
getTime()forInvalid Date:new Date("invalid")does not throw on construction; its.getTime()isNaN.
TL;DR: Call new Date().getFullYear() for the local 4-digit year (or new Date().getUTCFullYear() for UTC), and never use the deprecated getYear() method.
