JavaScript Date Get Methods
Learn how to get date and time values
🔍 What are Date Get Methods?
Date Get methods allow you to retrieve specific parts of a date object like year, month, day, hours, minutes, and seconds.
// Create a date and get its parts
let date = new Date("2024-03-15 14:30:25");
console.log(date.getFullYear()); // 2024
Common Get Methods
Date Parts
Get year, month, and day
let date = new Date("2024-03-15");
console.log(date.getFullYear()); // 2024
console.log(date.getMonth()); // 2 (0-based)
console.log(date.getDate()); // 15
Time Parts
Get hours, minutes, seconds
let date = new Date("2024-03-15 14:30:25");
console.log(date.getHours()); // 14
console.log(date.getMinutes()); // 30
console.log(date.getSeconds()); // 25
Day of Week
Get the day of the week
let date = new Date("2024-03-15");
console.log(date.getDay()); // 5 (Friday)
// 0=Sunday, 1=Monday, etc.
Milliseconds
Get precise time values
let date = new Date();
console.log(date.getTime()); // Milliseconds since 1970
console.log(date.getMilliseconds()); // 0-999
🔹 Basic Get Methods
Here are the most commonly used get methods:
let date = new Date("2024-03-15 14:30:25");
// Date components
console.log("Year:", date.getFullYear()); // 2024
console.log("Month:", date.getMonth()); // 2 (March = 2, 0-based)
console.log("Date:", date.getDate()); // 15
console.log("Day:", date.getDay()); // 5 (Friday)
// Time components
console.log("Hours:", date.getHours()); // 14
console.log("Minutes:", date.getMinutes()); // 30
console.log("Seconds:", date.getSeconds()); // 25
console.log("Milliseconds:", date.getMilliseconds()); // 0
Output:
Year: 2024
Month: 2
Date: 15
Day: 5
Hours: 14
Minutes: 30
Seconds: 25
Milliseconds: 0
🔹 Understanding Month Numbers
JavaScript months are 0-based (0 = January, 11 = December):
let date = new Date("2024-03-15");
// Month is 0-based
console.log("Month number:", date.getMonth()); // 2 (March)
// Convert to month name
let months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
console.log("Month name:", months[date.getMonth()]); // March
// Or use a function
function getMonthName(date) {
return months[date.getMonth()];
}
console.log("Month:", getMonthName(date)); // March
Output:
Month number: 2
Month name: March
Month: March
🔹 Day of Week Names
Convert day numbers to day names:
let date = new Date("2024-03-15");
// Day is 0-based (0 = Sunday)
console.log("Day number:", date.getDay()); // 5 (Friday)
// Convert to day name
let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday'];
console.log("Day name:", days[date.getDay()]); // Friday
// Function to get day name
function getDayName(date) {
return days[date.getDay()];
}
console.log("Today is:", getDayName(date)); // Today is: Friday
Output:
Day number: 5
Day name: Friday
Today is: Friday
🔹 UTC Get Methods
Get UTC (Universal Time) values instead of local time:
let date = new Date("2024-03-15 14:30:25");
// Local time methods
console.log("Local Hours:", date.getHours()); // 14
console.log("Local Minutes:", date.getMinutes()); // 30
// UTC time methods
console.log("UTC Hours:", date.getUTCHours()); // May differ based on timezone
console.log("UTC Minutes:", date.getUTCMinutes()); // 30
// All UTC methods
console.log("UTC Year:", date.getUTCFullYear());
console.log("UTC Month:", date.getUTCMonth());
console.log("UTC Date:", date.getUTCDate());
console.log("UTC Day:", date.getUTCDay());
Output:
Local Hours: 14
Local Minutes: 30
UTC Hours: 19 (example, depends on timezone)
UTC Minutes: 30
UTC Year: 2024
UTC Month: 2
UTC Date: 15
UTC Day: 5
🔹 Practical Examples
Real-world examples using get methods:
let now = new Date();
// Display current date and time
function displayDateTime() {
let hours = now.getHours().toString().padStart(2, '0');
let minutes = now.getMinutes().toString().padStart(2, '0');
let seconds = now.getSeconds().toString().padStart(2, '0');
let day = now.getDate().toString().padStart(2, '0');
let month = (now.getMonth() + 1).toString().padStart(2, '0');
let year = now.getFullYear();
console.log(`Time: ${hours}:${minutes}:${seconds}`);
console.log(`Date: ${day}/${month}/${year}`);
}
displayDateTime();
// Check if it's weekend
function isWeekend(date) {
let day = date.getDay();
return day === 0 || day === 6; // Sunday or Saturday
}
console.log("Is weekend?", isWeekend(now));
// Age calculator
function calculateAge(birthDate) {
let today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
// Check if birthday hasn't occurred this year
if (today.getMonth() < birthDate.getMonth() ||
(today.getMonth() === birthDate.getMonth() && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
let birthDate = new Date("1990-05-15");
console.log("Age:", calculateAge(birthDate));
Output:
Time: 14:30:25
Date: 15/03/2024
Is weekend? false
Age: 33