JavaScript
Programming
Web Development
Date Objects
Coding Tips

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.

Browse interview questions

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()

javascript
1const year = new Date().getFullYear();
2console.log(year); // 2026
3
4// Assign to a variable for reuse
5const now = new Date();
6console.log(now.getFullYear());  // 2026
7console.log(now.getMonth());     // 0-11 (0 = January)
8console.log(now.getDate());      // 1-31

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:

javascript
1const now = new Date();
2
3// Local timezone year
4console.log(now.getFullYear());      // depends on local time
5
6// UTC year
7console.log(now.getUTCFullYear());   // depends on UTC time
8
9// Example: At 11 PM EST on Dec 31, 2025
10// getFullYear() → 2025 (local: still Dec 31)
11// getUTCFullYear() → 2026 (UTC: already Jan 1)

For server-side code or databases, prefer getUTCFullYear() to avoid timezone-related bugs.

The most common use case — automatically updating the year in a website footer:

html
1<footer>
2  <p>&copy; <span id="year"></span> My Company</p>
3</footer>
4
5<script>
6  document.getElementById('year').textContent = new Date().getFullYear();
7</script>

Or inline with document.write (suitable only for simple static pages):

html
<p>&copy; <script>document.write(new Date().getFullYear())</script> My Company</p>

In React:

jsx
function Footer() {
  return <footer>&copy; {new Date().getFullYear()} My Company</footer>;
}

Using Intl.DateTimeFormat

Intl.DateTimeFormat provides locale-aware date formatting:

javascript
1// Year only
2const year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(new Date());
3console.log(year); // '2026'
4
5// Two-digit year
6const shortYear = new Intl.DateTimeFormat('en', { year: '2-digit' }).format(new Date());
7console.log(shortYear); // '26'
8
9// Non-Gregorian calendar year
10const japaneseYear = new Intl.DateTimeFormat('ja-JP-u-ca-japanese', { year: 'numeric' }).format(new Date());
11console.log(japaneseYear); // '8' (Reiwa 8 in 2026)

Extracting Year from a Date String

javascript
1// From an ISO string
2const dateStr = '2025-06-15T10:30:00Z';
3const year = new Date(dateStr).getFullYear();
4console.log(year); // 2025
5
6// From a timestamp (milliseconds since epoch)
7const timestamp = 1735689600000;
8const yearFromTs = new Date(timestamp).getFullYear();
9console.log(yearFromTs); // 2025
10
11// Parse year directly from string (no Date object)
12const yearOnly = parseInt('2025-06-15'.split('-')[0], 10);
13console.log(yearOnly); // 2025

Year Calculations

javascript
1const now = new Date();
2const currentYear = now.getFullYear();
3
4// Age calculation
5function getAge(birthYear) {
6  return currentYear - birthYear;
7}
8console.log(getAge(1990)); // 36 (in 2026)
9
10// Check leap year
11function isLeapYear(year) {
12  return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
13}
14console.log(isLeapYear(currentYear)); // false (2026 is not a leap year)
15
16// Year range
17const years = Array.from({ length: 10 }, (_, i) => currentYear - i);
18console.log(years); // [2026, 2025, 2024, ..., 2017]

Deprecated: getYear()

getYear() returns the year minus 1900, which is a legacy behavior from early JavaScript:

javascript
1const now = new Date();
2
3console.log(now.getYear());     // 126 (2026 - 1900) — DON'T USE
4console.log(now.getFullYear()); // 2026 — USE THIS

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 use getFullYear() 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. Use getUTCFullYear() when timezone consistency matters.
  • Month is zero-indexed: new Date(2026, 0, 1) is January 1st, not month 0. This does not affect getFullYear() but is a common source of date bugs in surrounding code.
  • Date parsing inconsistencies: new Date('2025-01-01') is parsed as UTC midnight, but new 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, call getFullYear() 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.DateTimeFormat with { 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.