Format the Date in JavaScript

This is a preview lesson
Register or sign in to take this lesson.

This tutorial shows how to format the date in JavaScript. For this, we can use the in-built Date() function.

Syntax

let date = new Date();
console.log(date)

Output

2022-05-23T08:28:09.699Z

The Date() function returns the current date and time. so, to format it we can use functions like getDate(), getMonth() and getFullYear to get the current date, month and year.

Example 1: Formatting the Date

// program to format the date
// get current  date
let date= new Date();

// get current day
let day = date.getDate();

// get current month

let month = date.getMonth() + 1;

// get current year
let year = date.getFullYear();

// display in various formats
const formattedDate = day + '/' + month + '/' + year;
console.log(formattedDate);

Output

23/5/2022