How to Get the Current Date in JavaScript?

In JavaScript, you can easily get the current date by simply calling the Date() constructor without passing any parameters to it eg. new Date().

When we call the Date() constructor without passing any parameters, it returns a Date object representing the current date and time.

For example,

// Current date object
const today = new Date();

console.log(today);
// Output: πŸ‘‰ Mon Mar 20 2023 14:38:39 GMT+0530 (India Standard Time)

The Date() constructor actually returns a date object, not a date string.

Therefore, we can call several built-in date methods on the returned date object to get the current year, month and day.

For example,

// Current date object
const today = new Date();

// Get the year, month & day
const year = today.getFullYear();
const month = today.getMonth() + 1;
const day = today.getDate();

// Format the date in 'dd/mm/yyyy'
const formattedDate = `${day}/${month}/${year}`;

console.log(formattedDate);
// Output: πŸ‘‰ "20/3/2023"

We have used three date methods in the above example:

  • getFullYear() – Returns an integer representing the year for the specified date
  • getMonth() – Returns an integer between 0 and 11 representing the month of the year. 0 corresponds to January, 1 corresponds to February, and so on.
  • getDate() – Returns an integer between 1 and 31 representing the day of the month.

Please note that the result of the getMonth() method is 0-based. Therefore, we add +1 to get the month number.

Since the result of the getDate() and getMonth() methods could also be single digits, therefore, you should use the padStart() method to pad the result with zeros for better formatting.

For example,

// Current date object
const today = new Date();

// Get the year, month & day
const year = today.getFullYear();
const month = (today.getMonth() + 1).toString().padStart(2, '0');
const day = today.getDate().toString().padStart(2, '0');

// Format the date in 'dd/mm/yyyy'
const formattedDate = `${day}/${month}/${year}`;

console.log(formattedDate);
// Output: πŸ‘‰ "20/03/2023"

Get the Current Time from the Current Date Object

To get the current time i.e. hours, minutes, and seconds, you can call the getHours(), getMinutes() and getSeconds() methods on the current date object returned by the Date() constructor.

Here is a short description of the three methods:

  • getHours() – Returns an integer between 0 and 23 representing the hour for the given date according to local time
  • getMinutes() – Returns an integer between 0 and 59 representing the minutes for the given date according to local time
  • getSeconds() – Returns an integer between 0 and 59 representing the seconds for the given date according to local time

See the following example,

// Current date object
const today = new Date();

// Get the hours, minutes & seconds
let hours = today.getHours();
let minutes = today.getMinutes();
let seconds = today.getSeconds();

// Pad beginning with zeros(if required)
hours = hours.toString().padStart(2, '0');
minutes = minutes.toString().padStart(2, '0');
seconds = seconds.toString().padStart(2, '0');

// Format the time in 'HH:mm:ss'
const currentTime = `${hours}:${minutes}:${seconds}`;

console.log(currentTime);
// Output: πŸ‘‰ "15:33:06"

Using toLocaleDateString() to Format the Current Date

The toLocaleDateString() method returns the string representation of a date according to the user’s local time zone.

For example,

// Current date object
const today = new Date();

console.log(today.toLocaleDateString());
// Output: πŸ‘‰ "3/20/2023"

You can also pass the user’s locale to the toLocaleDateString() method as an argument to get the date string in any specific format.

For example,

// Current date object
const today = new Date();

// Get date string in Indian std date format
const formattedDate = today.toLocaleDateString('en-IN');

console.log(formattedDate);
// Output: πŸ‘‰ "20/3/2023"

If you want even more customization over the date string, you can pass an options object as a second parameter to the toLocaleDateString() method.

For example,

// Current date object
const today = new Date();

const options = {
    weekday: "long",
    year: "numeric",
    month: "long",
    day: "numeric",
};

// Customize the date
const formattedDate = today.toLocaleDateString('en-IN', options);

console.log(formattedDate);
// Output: πŸ‘‰ "Monday, 20 March, 2023"

By customizing the options, you can get the date string in any desired date format.

That’s all for this article. Thanks for reading!