DEV Community

Cover image for How to get only the current date number using JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to get only the current date number using JavaScript?

Originally posted here!

To get only the current date number, we can use the getDate() method from the global Date object in JavaScript.

TL;DR

// Make an instance or object of the Date constructor
const currentDate = new Date();

// get the current date number
const dateNumber = currentDate.getDate();

console.log(dateNumber); // eg: 17
Enter fullscreen mode Exit fullscreen mode

First, let's make an object (or instance) from the global Date constructor using the new keyword like this,

// Make an instance or object of the Date constructor
const currentDate = new Date();
Enter fullscreen mode Exit fullscreen mode

Now we can use the getDate() method from the currentDate object to get the date number like this,

// Make an instance or object of the Date constructor
const currentDate = new Date();

// get the current date number
const dateNumber = currentDate.getDate();

console.log(dateNumber); // eg: 17
Enter fullscreen mode Exit fullscreen mode

Yay, We successfully got the date number 🎉!

See the above code live in JSBin.

That's all 😃!

Feel free to share if you found this useful 😃.


Top comments (0)