All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Current Date On JavaScript

Last Updated : Mar 11, 2024

How To Get Current Date On JavaScript

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>
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. The current year, month, and day values are printed to the console using the console.log() commands.
  6. console.log("Current Year:", cYear); prints the value of the current year in addition to it.
  7. console.log("Current Month:", cMonth); prints the value of the current month in addition to it.
  8. 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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪