How to get the current day, month, and year in JavaScript

The Date object methods getDate(), getMonth(), and getFullYear() can be used to retrieve day, month, and full year from a date object in JavaScript.

Here is an example:

const date = new Date(2021, 8, 18);

const day = date.getDate();
const month = date.getMonth() + 1; // getMonth() returns month from 0 to 11
const year = date.getFullYear();

const str = `${day}/${month}/${year}`;

console.log(str);
// 18/9/2021

Note that the getDate(), getMonth(), and getFullYear() methods return the date and time in the local time zone of the computer where the code is running.

To get the date and time in the universal timezone (UTC), just replace the above methods with the getUTCDate(), getUTCMonth(), and getUTCFullYear() respectively:

const date = new Date(2021, 8, 18);

const day = date.getUTCDate();
const month = date.getUTCMonth() + 1; // getUTCMonth() returns month from 0 to 11
const year = date.getUTCFullYear();

const str = `${day}/${month}/${year}`;

console.log(str);
// 18/9/2021

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.