-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
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.
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.
JavaScript provides multiple get methods to retrieve various components of a Date object. Here are the most commonly used:
Returns the four-digit year of the date.
let today = new Date();
console.log(today.getFullYear()); // Example: 2025
Returns the month (0-11). January is 0, December is 11.
let today = new Date();
console.log(today.getMonth()); // Example: 11 for December
Returns the day of the month (1-31).
let today = new Date();
console.log(today.getDate()); // Example: 23
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
Returns the hour of the day (0-23).
let now = new Date();
console.log(now.getHours()); // Example: 14
Returns the minutes (0-59).
let now = new Date();
console.log(now.getMinutes()); // Example: 30
Returns the seconds (0-59).
let now = new Date();
console.log(now.getSeconds()); // Example: 45
Returns the milliseconds (0-999).
let now = new Date();
console.log(now.getMilliseconds()); // Example: 123
Returns the number of milliseconds since January 1, 1970 (epoch time).
let now = new Date();
console.log(now.getTime());
Returns the difference in minutes between UTC and local time.
let now = new Date();
console.log(now.getTimezoneOffset()); // Example: -330 for IST
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()}`);
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`);
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}`);
let now = new Date();
console.log(`Current time: ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`);
let start = new Date();
console.log(`Activity started at: ${start.getTime()} milliseconds since epoch`);
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()]}`);
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
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
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
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.
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?
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
