Table of Contents
Dates are an essential aspect of many web applications. Whether you need to display the current date, calculate durations, or handle time-sensitive events, JavaScript’s date methods come in handy. These methods provide a straightforward way to work with dates, allowing you to extract specific information or modify date objects as needed.
Understanding Date Objects in JavaScript
Before diving into the various date methods, it’s important to understand how dates are represented in JavaScript. In JavaScript, dates are represented as objects of the Date class. These date objects hold the date and time information, which can be accessed using specific methods.
Date Methods
The getDate() Method
The getDate() method is used to retrieve the day of the month from a date object. It returns a value between 1 and 31, representing the current day.
var currentDate = new Date();
var day = currentDate.getDate();
console.log("Current day: " + day);
The getMonth() Method
The getMonth() method returns the month of a date object as a number between 0 and 11. It means January is represented by 0, February by 1, and so on.
var currentDate = new Date();
var month = currentDate.getMonth();
console.log("Current month: " + month);
The getFullYear() Method
The getFullYear() method returns the four-digit year of a date object. It provides the full year instead of a two-digit representation.
var currentDate = new Date();
var year = currentDate.getFullYear();
console.log("Current year: " + year);
The getDay() Method
The getDay() method returns the day of the week as a number between 0 and 6. Here, Sunday is represented by 0, Monday by 1, and so on.
var currentDate = new Date();
var dayOfWeek = currentDate.getDay();
console.log("Day of the week: " + dayOfWeek);
The getHours() Method
The getHours() method retrieves the hour from a date object as a number between 0 and 23, representing the current hour in the 24-hour format.
var currentTime = new Date();
var hour = currentTime.getHours();
console.log("Current hour: " + hour);
The getMinutes() Method
The getMinutes() method returns the minutes of a date object as a number between 0 and 59, representing the current minute.
var currentTime = new Date();
var minutes = currentTime.getMinutes();
console.log("Current minutes: " + minutes);
The getSeconds() Method
The getSeconds() method retrieves the seconds from a date object as a number between 0 and 59, representing the current second.
var currentTime = new Date();
var seconds = currentTime.getSeconds();
console.log("Current seconds: " + seconds);
Manipulating Dates with JavaScript
Apart from retrieving information from date objects, JavaScript also allows you to manipulate dates by modifying their components. Let’s explore some commonly used methods for date manipulation:
The setDate() Method
The setDate() method is used to set the day of the month for a date object. It takes a numeric value between 1 and 31 as an argument and updates the date object accordingly.
var currentDate = new Date();
currentDate.setDate(15);
console.log("Updated date: " + currentDate);
The setMonth() Method
The setMonth() method allows you to change the month of a date object. It takes a numeric value between 0 and 11, where 0 represents January and 11 represents December.
var currentDate = new Date();
var year = currentDate.getFullYear();
console.log("Current year: " + year);
The setFullYear() Method
The setFullYear() method is used to set the year of a date object. It takes a four-digit numeric value as an argument and updates the year component of the date accordingly.
var currentDate = new Date();
currentDate.setFullYear(2022);
console.log("Updated year: " + currentDate.getFullYear());
The setHours() Method
The setHours() method allows you to set the hour of a date object. It takes a numeric value between 0 and 23 and updates the hour component accordingly.
var currentTime = new Date();
currentTime.setHours(15);
console.log("Updated hour: " + currentTime.getHours());
The setMinutes() Method
The setMinutes() method is used to set the minutes of a date object. It takes a numeric value between 0 and 59 and updates the minutes component accordingly.
var currentTime = new Date();
currentTime.setMinutes(30);
console.log("Updated minutes: " + currentTime.getMinutes());
The setSeconds() Method
The setSeconds() method allows you to set the seconds of a date object. It takes a numeric value between 0 and 59 and updates the seconds component accordingly.
var currentTime = new Date();
currentTime.setSeconds(45);
console.log("Updated seconds: " + currentTime.getSeconds());
Conclusion
In conclusion, JavaScript provides powerful built-in methods to work with dates and times. By leveraging these date methods, developers can easily extract specific date components, such as the day, month, year, hours, minutes, and seconds, from date objects. Additionally, JavaScript also enables the manipulation of dates by allowing the modification of individual date components. This flexibility empowers developers to create dynamic and time-sensitive web applications with ease.
Remember to practice using these date methods in JavaScript to become more comfortable and proficient in handling dates within your projects.
FAQs
How do I extract the day from a JavaScript date object?
You can use the getDay() method to extract the day from a JavaScript date object. It returns a value between 0 and 6, where 0 represents Sunday, 1 represents Monday, and so on.
How can I change the month in a JavaScript date object?
You can change the month in a JavaScript date object using the setMonth() method. It takes a numeric value between 0 and 11, where 0 represents January and 11 represents December.
How do I set the year in a JavaScript date object?
To set the year in a JavaScript date object, you can use the setFullYear() method. It takes a four-digit numeric value as an argument.
Can I set the time in a JavaScript date object?
Yes, you can set the time in a JavaScript date object by using the appropriate set methods like setHours(), `setMinutes()

