Get the current year in JavaScript
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting the current year in JavaScript is a common task for displaying copyright notices, generating timestamps, calculating ages, and filtering date-based data. The Date object's getFullYear() method is the standard approach, but there are important distinctions between local time and UTC, and modern alternatives like Intl.DateTimeFormat provide locale-aware formatting.
Basic: getFullYear()
new Date() creates a Date object set to the current date and time. getFullYear() returns the four-digit year according to the local timezone.
UTC vs Local Year
Near midnight on December 31st or January 1st, the local year and UTC year may differ depending on your timezone:
For server-side code or databases, prefer getUTCFullYear() to avoid timezone-related bugs.
Dynamic Copyright Notice
The most common use case — automatically updating the year in a website footer:
Or inline with document.write (suitable only for simple static pages):
In React:
Using Intl.DateTimeFormat
Intl.DateTimeFormat provides locale-aware date formatting:
Extracting Year from a Date String
Year Calculations
Deprecated: getYear()
getYear() returns the year minus 1900, which is a legacy behavior from early JavaScript:
getYear() is deprecated and should never be used. Always use getFullYear().
Common Pitfalls
- getYear() vs getFullYear():
getYear()returns year - 1900 (e.g., 126 for 2026). Always usegetFullYear()for the four-digit year. - Timezone at year boundary:
getFullYear()uses the local timezone. Near midnight on Dec 31/Jan 1, the year may differ between local and UTC. UsegetUTCFullYear()when timezone consistency matters. - Month is zero-indexed:
new Date(2026, 0, 1)is January 1st, not month 0. This does not affectgetFullYear()but is a common source of date bugs in surrounding code. - Date parsing inconsistencies:
new Date('2025-01-01')is parsed as UTC midnight, butnew Date('01/01/2025')is parsed as local midnight. This can cause the year to shift by one day depending on timezone. Prefer ISO 8601 format (YYYY-MM-DD) with explicit timezone handling. - Caching the year: If you store
new Date().getFullYear()in a module-level constant, it will reflect the year when the module was first loaded, not the current year. For long-running Node.js servers, callgetFullYear()at request time, not at startup.
Summary
- Use
new Date().getFullYear()to get the current four-digit year in local time - Use
getUTCFullYear()for timezone-independent year in server-side or database code - Use
Intl.DateTimeFormatwith{ year: 'numeric' }for locale-aware formatting - Never use the deprecated
getYear()— it returns year minus 1900 - For dynamic copyright notices, inject
new Date().getFullYear()into the DOM
Related reading
- Get the delta of two javascript objects
- Get the first element of an array
- Get the full call stack trace of http calls
- Get the value in an input text box
- Get the value of URL `Parameters`
- Getting a random value from a JavaScript array
- Getting an Access Denied error when I reload my React app on AWS Amplify
- getting schema attributes from Mongoose Model
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.