
JavaScript’s Date object is one of those tools you rely on constantly but rarely give much thought to—until it starts behaving unexpectedly. The whole point of Date is to represent a single moment in time, internally tracked as the number of milliseconds elapsed since the UNIX epoch: January 1, 1970, 00:00:00 UTC. This makes it simpler to do math with dates—but also means you always need to be conscious of time zones.
Creating a new Date is as simple as calling the constructor. Without arguments, you’ll get the current local date and time:
const now = new Date(); console.log(now.toString()); // e.g., "Tue Jun 04 2024 15:42:17 GMT-0400 (Eastern Daylight Time)"
You can also supply a specific date and time in a variety of formats—strings, milliseconds, or components representing year, month, day, and so forth:
// Year, month (0-based), day, hour, minute, second, millisecond const specificDate = new Date(2024, 5, 4, 15, 30); // June 4, 2024, 15:30 local time console.log(specificDate.toISOString()); // In UTC: "2024-06-04T19:30:00.000Z"
Notice that months are zero-based, which trips up plenty of developers. Remember, 0 is January and 11 is December—so when you want June, you’ll pass in 5. This inconsistency is a relic of JavaScript’s early days but still something you just have to live with.
What makes the Date object tricky isn’t constructing it—that’s straightforward—it’s how it handles time zones and the ambiguity in string parsing. When you pass a date string, JavaScript assumes UTC for ISO 8601 formats (like “2024-06-04T15:30:00Z”) but local time for ambiguous ones (like “June 4, 2024 15:30”), which can cause headaches during daylight saving transitions or when running code on servers in different time zones.
Beyond that, the Date object gives you plenty of methods to pull pieces out or manipulate the stored timestamp. Some methods consider local time, others UTC, so mixing them without understanding the difference is a recipe for bugs. For example:
const date = new Date("2024-06-04T15:30:00Z");
console.log(date.getHours()); // Local time hour
console.log(date.getUTCHours()); // Hours in UTC
Usually, if you want to display or work with dates relative to users, you’ll prefer local methods like getFullYear(), getMonth(), and getDate(). But for server-side calculations or standardized timestamps, UTC methods come into play.
It’s also worth noting that the Date object is mutable:
const d = new Date(2024, 0, 1); d.setMonth(d.getMonth() + 1); console.log(d.toDateString()); // "Sat Feb 01 2024"
This allows you to shift dates forward or back by months, days, or even milliseconds. But be careful when chaining changes because internal adjustments will roll over months or days automatically.
That internal millisecond count is the real magic behind Date. You can extract it with getTime() and base all your date arithmetic on that, gaining precision and reducing ambiguity:
const start = new Date(2024, 0, 1); const end = new Date(2024, 5, 4); const diffInMs = end.getTime() - start.getTime(); const diffInDays = diffInMs / (1000 * 60 * 60 * 24); console.log(diffInDays); // Approximate number of days between the two dates
That’s the essence of JavaScript dates. Think of the Date object as a thin wrapper around a 64-bit integer representing the number of milliseconds elapsed, plus a set of utility methods for wrestling that into something human-readable or computationally useful. If you want to work with individual pieces, pull them out or set them through the local or UTC flavor of methods, but always keep in mind the underlying time zone context.
Next up, let’s explore how to retrieve just the year from a Date object using those built-in methods, keeping all the gotchas in mind.
Ubluker 10K 8K 4K HDMI Cable 48Gbps 5 FT, Certified Ultra High Speed HDMI® Cable 4K 240Hz 144Hz 120Hz 8K60Hz 0.01ms HDR10+ eARC HDCP2.3 Netflix Roku TV PC Monitor Projector PS5 Xbox
$8.36 (as of June 2, 2026 22:39 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Using built-in methods to retrieve the year
To retrieve the year from a Date object, JavaScript provides the getFullYear() method. This returns a four-digit year according to the local time zone of the environment where the code runs. For instance:
const date = new Date("2024-06-04T15:30:00Z");
console.log(date.getFullYear()); // 2024
Notice the contrast to the deprecated getYear() method, which you should avoid altogether. getYear() returns the year minus 1900, so 2024 would come back as 124—this is a legacy artifact from very old JavaScript versions and should never appear in modern code.
If you want the year in UTC rather than local time, you can use getUTCFullYear(). This very important when working with dates in a global context or when the local time zone might shift the effective year due to time offsets:
const utcDate = new Date("2024-01-01T00:30:00+02:00");
// Local time might be late December 31 for some zones
console.log(utcDate.getFullYear()); // local year, possibly 2023
console.log(utcDate.getUTCFullYear()); // UTC year, 2024
That difference is sometimes subtle but can cause bugs when you’re comparing or sorting dates where the local date “falls back” a day due to time zone differences.
When creating your own date-handling logic, beware of the zero-based month index alongside the simpler year extraction. For example, if you want to parse a user input string like “April 15, 2024” into a Date object and then get the year:
const userInput = "April 15, 2024"; const parsedDate = new Date(userInput); console.log(parsedDate.getFullYear()); // 2024
While this usually works, remember that relying on the Date string parser can be inconsistent across browsers and locales, so the recommended approach is to explicitly parse or use libraries for complex inputs.
Another handy tip: when formatting or displaying the year, avoid manually slicing strings from toString() or toISOString(). Instead, use getFullYear() to ensure accuracy and clarity in your code:
const d = new Date();
const year = d.getFullYear();
console.log("Current year is " + year);
This method works consistently regardless of how the date was initially created, as long as the Date is valid.
