JavaScript's Date object provides several methods to get information about dates and times.
These methods make it easy to retrieve specific components such as the year, month, day, hour, minute, and more.
This tutorial will guide you through the various get methods provided by the Date object, with practical examples.
Table of Contents
1. Creating a Date Object
Before using get methods, you need a Date object. You can create a Date object representing the current date and time or a specific date.
Example 1: Creating a Date Object
// Current date and time const now = new Date(); console.log(now); // Output: Current date and time // Specific date and time (Year, Month (0-based), Day, Hours, Minutes, Seconds) const specificDate = new Date(2024, 9, 6, 14, 30, 0); // October 6, 2024, 14:30:00 console.log(specificDate); // Output: Sun Oct 06 2024 14:30:00 GMT+0000 (Coordinated Universal Time)
2. getFullYear() Method
The getFullYear() method returns the year of a Date object as a four-digit number.
Example 2: Using getFullYear()
const date = new Date(); console.log(date.getFullYear()); // Output: Current year (e.g., 2024)
3. getMonth() Method
The getMonth() method returns the month of a Date object as a zero-based number (0-11), where 0 represents January and 11 represents December.
Example 3: Using getMonth()
const date = new Date(); console.log(date.getMonth()); // Output: Current month as a zero-based number (e.g., 9 for October)
Note: Since months are zero-based, you may need to add 1 to get the “human-readable” month (1-12).
console.log(date.getMonth() + 1); // Output: Current month as 1-based (e.g., 10 for October)
4. getDate() Method
The getDate() method returns the day of the month (1-31) of a Date object.
Example 4: Using getDate()
const date = new Date(); console.log(date.getDate()); // Output: Current day of the month (e.g., 6)
5. getDay() Method
The getDay() method returns the day of the week (0-6), where 0 represents Sunday and 6 represents Saturday.
Example 5: Using getDay()
const date = new Date(); console.log(date.getDay()); // Output: Current day of the week as a number (e.g., 0 for Sunday)
6. getHours() Method
The getHours() method returns the hour (0-23) of a Date object, where 0 represents midnight and 23 represents 11 PM.
Example 6: Using getHours()
const date = new Date(); console.log(date.getHours()); // Output: Current hour (e.g., 14 for 2 PM)
7. getMinutes() Method
The getMinutes() method returns the minutes (0-59) of a Date object.
Example 7: Using getMinutes()
const date = new Date(); console.log(date.getMinutes()); // Output: Current minutes (e.g., 30)
8. getSeconds() Method
The getSeconds() method returns the seconds (0-59) of a Date object.
Example 8: Using getSeconds()
const date = new Date(); console.log(date.getSeconds()); // Output: Current seconds (e.g., 15)
9. getMilliseconds() Method
The getMilliseconds() method returns the milliseconds (0-999) of a Date object.
Example 9: Using getMilliseconds()
const date = new Date(); console.log(date.getMilliseconds()); // Output: Current milliseconds (e.g., 456)
10. getTime() Method
The getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC (the Unix epoch).
Example 10: Using getTime()
const date = new Date(); console.log(date.getTime()); // Output: Current timestamp in milliseconds (e.g., 1696595401234)
Explanation: This value is useful for calculating time differences.
const start = new Date(); // Some code execution here const end = new Date(); console.log(`Execution time: ${end.getTime() - start.getTime()} ms`);
11. getTimezoneOffset() Method
The getTimezoneOffset() method returns the difference in minutes between the local time zone and UTC.
Example 11: Using getTimezoneOffset()
const date = new Date(); console.log(date.getTimezoneOffset()); // Output: Difference in minutes (e.g., -240 for UTC-4)
Explanation: The return value is positive if the local time zone is behind UTC, and negative if it is ahead of UTC.
12. getUTC*() Methods
The getUTC*() methods provide the same functionality as their non-UTC counterparts but return the values in Coordinated Universal Time (UTC).
Example 12: Using getUTCFullYear(), getUTCMonth(), and getUTCDate()
const date = new Date(); console.log(date.getUTCFullYear()); // Output: Current year in UTC console.log(date.getUTCMonth()); // Output: Current month in UTC (0-based) console.log(date.getUTCDate()); // Output: Current day of the month in UTC console.log(date.getUTCHours()); // Output: Current hour in UTC console.log(date.getUTCMinutes()); // Output: Current minutes in UTC console.log(date.getUTCSeconds()); // Output: Current seconds in UTC console.log(date.getUTCMilliseconds()); // Output: Current milliseconds in UTC
13. Formatting a Full Date
You can combine these get methods to extract and format a full date string.
Example 13: Formatting a Full Date
const date = new Date(); const year = date.getFullYear(); const month = date.getMonth() + 1; // Months are 0-based, add 1 for human-readable format const day = date.getDate(); const hours = date.getHours(); const minutes = date.getMinutes(); const seconds = date.getSeconds(); console.log(`${year}-${month}-${day} ${hours}:${minutes}:${seconds}`); // Output: Formatted date string (e.g., '2024-10-6 14:30:15')
Explanation: This example extracts all components of the current date and formats them into a more readable string.
Conclusion
The Date object in JavaScript provides a variety of get methods to access different parts of a date and time.
Whether you need to retrieve the year, month, day, or time down to the millisecond, these methods provide an easy way to work with date data.
By understanding and using these methods effectively, you can easily handle dates and times in your JavaScript projects.
Feel free to experiment with these examples and use them as a reference for handling dates in your code!