JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Date Get Methods


Retrieving specific components from a date is a common requirement in JavaScript. Whether you are calculating ages, determining weekdays, showing user activity times, or creating event schedules, the Date object provides built-in “get” methods to access the year, month, day, hours, minutes, seconds, and milliseconds. These methods allow developers to manipulate and display date information efficiently.

In this tutorial, you will learn how to use JavaScript date get methods, with practical examples, common mistakes, best practices, and real-world applications.

Why Date Get Methods Are Important

Get methods are essential because they allow you to extract parts of a date:

  • Displaying formatted dates to users

  • Calculating age, time differences, or countdowns

  • Generating reports based on specific date components

  • Controlling logic based on weekdays or hours

  • Performing calculations on date and time values

Without these methods, you would have to manually parse date strings, which is inefficient and error-prone.

Common Get Methods

JavaScript provides multiple get methods to retrieve various components of a Date object. Here are the most commonly used:

getFullYear()

Returns the four-digit year of the date.

let today = new Date();
console.log(today.getFullYear()); // Example: 2025

getMonth()

Returns the month (0-11). January is 0, December is 11.

let today = new Date();
console.log(today.getMonth()); // Example: 11 for December

getDate()

Returns the day of the month (1-31).

let today = new Date();
console.log(today.getDate()); // Example: 23

getDay()

Returns the day of the week (0-6). Sunday is 0, Saturday is 6.

let today = new Date();
console.log(today.getDay()); // Example: 2 for Tuesday

getHours()

Returns the hour of the day (0-23).

let now = new Date();
console.log(now.getHours()); // Example: 14

getMinutes()

Returns the minutes (0-59).

let now = new Date();
console.log(now.getMinutes()); // Example: 30

getSeconds()

Returns the seconds (0-59).

let now = new Date();
console.log(now.getSeconds()); // Example: 45

getMilliseconds()

Returns the milliseconds (0-999).

let now = new Date();
console.log(now.getMilliseconds()); // Example: 123

getTime()

Returns the number of milliseconds since January 1, 1970 (epoch time).

let now = new Date();
console.log(now.getTime());

getTimezoneOffset()

Returns the difference in minutes between UTC and local time.

let now = new Date();
console.log(now.getTimezoneOffset()); // Example: -330 for IST

Practical Examples

Example 1: Display Current Date Components

let today = new Date();
console.log(`Year: ${today.getFullYear()}`);
console.log(`Month: ${today.getMonth() + 1}`); // Adding 1 because months are zero-based
console.log(`Date: ${today.getDate()}`);
console.log(`Day of Week: ${today.getDay()}`);

Example 2: Calculate Age from Birthdate

let birthDate = new Date("2003-07-10");
let today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
let monthDiff = today.getMonth() - birthDate.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
    age--;
}
console.log(`Age: ${age} years`);

Example 3: Countdown to Event

let eventDate = new Date("2025-12-31");
let today = new Date();
let diff = eventDate.getTime() - today.getTime();
let remainingDays = Math.ceil(diff / (1000 * 60 * 60 * 24));
console.log(`Days remaining: ${remainingDays}`);

Example 4: Show Current Time

let now = new Date();
console.log(`Current time: ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`);

Example 5: Logging Activity with Milliseconds

let start = new Date();
console.log(`Activity started at: ${start.getTime()} milliseconds since epoch`);

Example 6: Checking Weekday for Scheduling

let meetingDate = new Date("2025-12-25");
let weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
console.log(`Meeting is scheduled on: ${weekdays[meetingDate.getDay()]}`);

Common Mistakes

  • Forgetting that months are zero-based in getMonth()

  • Using getDate() when getDay() is needed, and vice versa

  • Ignoring time zones when calculating durations

  • Relying on string parsing instead of get methods

  • Not converting milliseconds properly for calculations

Best Practices

  • Use getFullYear() for year calculations to avoid issues with getYear()

  • Add 1 to the value from getMonth() when displaying months

  • Always validate user input when creating dynamic dates

  • Use getTime() for difference calculations between dates

  • Combine get methods to format dates for display or logging

Real-World Applications

Get methods are used in:

  • Calendar and scheduling applications

  • Age calculation tools

  • Event countdowns and reminders

  • Logging and monitoring timestamps

  • Generating reports with specific date filters

  • Time-based access or permissions

Summary of JS Date Get Methods

JavaScript’s date get methods provide a reliable way to extract specific components from a Date object. Using these methods, developers can access the year, month, day, hours, minutes, seconds, milliseconds, and time zone offset. Proper use of get methods allows for accurate calculations, formatted displays, and efficient time-based logic in web applications. By following best practices and avoiding common mistakes, you can manage dates effectively and build robust, user-friendly applications.


Practice Questions

Q1. How do you get the full year (e.g., 2025) from a Date object?

Q2. What will getMonth() return for the month of July?

Q3. How do you extract the day of the month (like 10) from a date?

Q4. Which method gives the day of the week as a number (0 for Sunday)?

Q5. How can you retrieve the hour (24-hour format) from a Date object?

Q6. Which method gives you the number of minutes from the Date object?

Q7. How do you get the current time in milliseconds since Jan 1, 1970?

Q8. What method returns the difference in time between UTC and local time (in minutes)?

Q9. What will getSeconds() return if the time is "2025-07-10T14:45:30"?

Q10. How can you get the UTC full year from a Date object?


Try a Short Quiz.

No quizzes available.


JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top