JavaScript Date Get Methods

What is JS Date Get Methods?

JavaScript Date objects provide a powerful set of methods to retrieve specific components of a date and time. These methods, known as Get Methods, are crucial for extracting information such as the year, month, day, hours, minutes, seconds and more from a Date object.

Overview of JavaScript Date Get Methods

The Date object includes the following get methods to retrieve date and time values:

MethodPurposeOutput
getFullYear()Returns the year in four digits2024
getMonth()Returns the month (0-11, where 0 = January)0 for January
getDate()Returns the day of the month (1-31)25
getDay()Returns the day of the week (0-6, where 0 = Sunday)1 for Monday
getHours()Returns the hour (0-23)15 for 3 PM
getMinutes()Returns the minutes (0-59)45
getSeconds()Returns the seconds (0-59)30
getMilliseconds()Returns the milliseconds (0-999)500
getTime()Returns the timestamp (milliseconds since 1970)1698259380000
getTimezoneOffset()Returns the timezone difference in minutes-330 for IST

Detailed Explanation of Each Method

1. getFullYear()

This method retrieves the four-digit year from a Date object.

Example:

const date = new Date();
console.log(date.getFullYear()); // Output: 2024

2. getMonth()

This method returns the month as a zero-based value (0 = January, 11 = December).

Example:

const date = new Date();
console.log(date.getMonth()); // Output: 10 (November)

To display the correct month, add 1:

console.log(date.getMonth() + 1); // Output: 11

3. getDate()

This method returns the day of the month (1-31).

Example:

const date = new Date();
console.log(date.getDate()); // Output: 25

4. getDay()

This method returns the day of the week as a zero-based value (0 = Sunday, 6 = Saturday).

Example:

const date = new Date();
console.log(date.getDay()); // Output: 1 (Monday)

To display the day name:

const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
console.log(days[date.getDay()]); // Output: Monday

5. getHours()

This method retrieves the hour from the Date object in a 24-hour format (0-23).

Example:

const date = new Date();
console.log(date.getHours()); // Output: 15 (3 PM)

6. getMinutes()

This method returns the minutes of the current hour (0-59).

Example:

const date = new Date();
console.log(date.getMinutes()); // Output: 45

7. getSeconds()

This method retrieves the seconds of the current minute (0-59).

Example:

const date = new Date();
console.log(date.getSeconds()); // Output: 30

8. getMilliseconds()

This method returns the milliseconds of the current second (0-999).

Example:

const date = new Date();
console.log(date.getMilliseconds()); // Output: 500

9. getTime()

This method returns the number of milliseconds since January 1, 1970 (epoch time).

Example:

const date = new Date();
console.log(date.getTime()); // Output: 1732598730000

10. getTimezoneOffset()

This method returns the difference between UTC and the local time in minutes.

Example:

const date = new Date();
console.log(date.getTimezoneOffset()); // Output: -330 (for IST)

Practical Examples

Example 1: Display Current Date and Time

const date = new Date();
const fullDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
const time = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
console.log(`Current Date: ${fullDate}`);
console.log(`Current Time: ${time}`);
// Output:
// Current Date: 2024-11-25
// Current Time: 15:45:30

Example 2: Calculate Age from a Birthdate

function calculateAge(birthDate) {
const today = new Date();
const birth = new Date(birthDate);
let age = today.getFullYear() - birth.getFullYear();
const monthDiff = today.getMonth() - birth.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
age--;
}
return age;
}

console.log(calculateAge("2000-05-15")); // Output: 24

Example 3: Convert Timestamp to Readable Date

const timestamp = 1732598730000;
const date = new Date(timestamp);
console.log(date.toLocaleDateString("en-US")); // Output: 11/25/2024
console.log(date.toLocaleTimeString("en-US")); // Output: 3:45:30 PM

Leave a Comment