In this article, I am going to show you how to get and display the current date in any format in JavaScript.

Step #1: Getting the date at the current time

const now = new Date();

Step #2: Displaying the current date

Example #1: The human-readable full description

// Tue Jun 09 2020 04:50:35 GMT+0700 (Indochina Time)
const text = now.toString();

Example #2: The human-readable date portion

// Tue Jun 09 2020
const text = now.toDateString();

Example #3: The ISO 8601 format

// 2020-06-08T21:50:35.573Z
const text = now.toISOString();

Example #4: The “YYYY-MM-DD” format (the local timezone)

const year = now.getFullYear();
const month = now.getMonth() + 1; // zero-based value, 0 = January
const day = now.getDate();
const padZero = value => value.toString().padStart(2, '0');

// 2020-06-09
const text = [year, month, day].map(padZero).join('-');

Example #5: The “YYYY-MM-DD” format (UTC+00:00)

const year = now.getUTCFullYear();
const month = now.getUTCMonth() + 1; // zero-based value, 0 = January
const day = now.getUTCDate();
const padZero = value => value.toString().padStart(2, '0');

// 2020-06-08
const text = [year, month, day].map(padZero).join('-');

Note: you only need to replace the get*() methods with the getUTC*() ones to get the corresponding UTC values.

Bonus tip: Date.prototype.toISOString() always returns a string with zero UTC offset (the letter Z at the end), so you can extract the date portion from the string result.

// 2020-06-08
const text = now.toISOString().substring(0, 10);