This post will discuss how to get the current date and time in JavaScript.

To get the current date and time in JavaScript, you can use the toLocaleString() method, which returns a string representing the given date according to language-specific conventions.

Download  Run Code

 
The toLocaleString() can be customized using the locales and the options argument:

Download  Run Code

 
To display only the date in a specific format, you can use the toLocaleDateString() method, as shown below:

Download  Run Code

 
To display only the time, you can use the toLocaleTimeString() method.

Download  Run Code

 
If you’re already using the Moment.js library, consider using the format() method to parse a moment.

Download Code

That’s all about getting the current date and time in JavaScript.