JavaScript

How to Get Current Date & Time in JavaScript?

Javascript has become a massively used programming language due to the expansion of the internet and the web at an unbelievable pace. In the modern world of the web, we can do almost every task in one single browser, and Javascript is used in every single website we see in our daily routine life. We frequently used to see the date and time at almost every website. In this article, we are going to have a look at how we can get the current time in Javascript and what are the different ways to get the date and time according to our requirement.

Javascript provides a built-in object Date, which helps in managing all the date and time. Later, we can extract whatever we want according to our needs using different built-in methods. So, let’s just straight jump into the process and learn the different techniques to extract the current date and time.

First of all, we will create a new object of Date() and declare a variable named “current” and assign the new Object of Date() to a “current” variable.

var current = new Date();

After assigning, let’s have a look at the object Date what does it have for us.

console.log(current)

Alright! It looks pretty cool in a good format. But, how about if we want to get only the year from the entire date? We can use the built-in function getFullYear() for getting the year only.

current.getFullYear();

Similarly, if we want to extract only the year, we can use the built-in function getMonths() for getting the month only.

current.getMonth();

There seems like an issue. This is not the 8th month(August)! As we can see in the above complete output for the new Date object. This is September. Well, this is because of the digital(0-11). So, we have to add “1” to it for getting the right month every time.

current.getMonth() + 1;

This is fine now.

Just like for the year, we can do the same for the date. For example, to extract or get only the date, we can use the built-in function getDate().

current.getDate();

Just like a date, we have built-in functions for extracting the desired piece of time. For example, if we want to get or extract the hours only, from the whole current time, we can use the built-in function getHours().

current.getHours();

The same goes for the minutes. To extract minutes only, we can use getMinutes().

current.getMinutes();

To extract seconds only, we can use getSeconds().

current.getSeconds();

Advanced built-in functions

Here we have some advanced built-in functions to get the date and time in a pretty clean and good formatted string. For example, in order to get only the time, not the date, in the form of string we can use the built-in function toLocaleTimeString() to our purpose.

current.toLocaleTimeString(); // "2:42:07 PM"

And, if we want to extract only the time in the form of string. We can use the built-in function toLocaleDateString().

current.toLocaleDateString(); // "9/29/2020"

And, if we want to extract both the date and time in a single string, we can use the built-in function toLocaleString().

current.toLocaleString(); // "9/29/2020, 2:42:07 PM"

So, this is how we can get the date and time using the built-in date object and extract the required months, years, or minutes using different methods.

Conclusion

This article explains how we can get the current date and time and how we can use it to our needs in a very easy, profound, and effective way that any beginner can understand and use. So, keep on learning, working, and getting experience in Javascript with linuxhint.com to have a better grasp over it. Thank you so much!

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.