How To Get Current Date On JavaScript
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Pragati
In this article we will show you the solution of how to get current date on JavaScript, in this section, we will examine the built-in Date object and its methods for retrieving both the whole current date and specific date elements like the year, month, day.
You will have a thorough understanding of the process to access and modify various elements of the present date by the conclusion of this course.
JavaScript is a potent language used in web development that enables programmers to build dynamic and interactive webpages.
Getting the current date is a basic job in many online applications.
Having the ability to access the current date is crucial for conducting date-related computations, logging timestamps, and presenting the date to users.
In this article, we will examine different ways to get the present date in JavaScript along with how to split its components.
We will largely use the Date object, an essential component of JavaScript that offers techniques for manipulating dates and times, to accomplish our purpose.
To extract distinct parts of the current date, we will use a variety of Date object methods, and we will then break down each part's relevance step by step.
Step By Step Guide On How To Get Current Date On JavaScript :-
<script> const cDate = new Date(); const cYear = cDate.getFullYear(); const cMonth = cDate.getMonth() + 1; // Months are 0-based, so add 1 const cDay = cDate.getDate(); console.log("Current Year:", cYear); console.log("Current Month:", cMonth); console.log("Current Day:", cDay); </script>
- Constant variable cDate is given a new instance within the Date object by the line const cDate = new Date();. The time and date are represented by this object.
- The formula is const cYear = cDate.getFullYear(); The current year is retrieved using the getFullYear() function from the Date object in the given line. After that, the value is kept in the cYear constant variable.
- The current month is obtained using the getMonth() method in the given line const cMonth = cDate.getMonth() + 1;. As a result, we must add 1 to obtain the actual month number in JavaScript because months are zero-based (0 to 11). The result is kept in the cMonth constant variable.
- Const cDay = cDate.getDate(); retrieves the day of the month from a Date object using the getDate() function. The cDay constant variable holds the value.
- The current year, month, and day values are printed to the console using the console.log() commands.
- console.log("Current Year:", cYear); prints the value of the current year in addition to it.
- console.log("Current Month:", cMonth); prints the value of the current month in addition to it.
- console.log("Current Day:", cDay); prints the value of the current date in addition to it.
Conclusion :-
In this article, we looked at different ways to get the current date using JavaScript and learnt how to break out its distinct parts.
We may quickly retrieve the year, month date of the current date by using the Date object and its related methods.
This information is crucial for a variety of web development duties, including providing users with real-time information, logging timestamps, and carrying out time-based computations.
I hope this article on how to get current date on javascript helps you and the steps and method mentioned above are easy to follow and implement.