Example 1: Format The Date
Output
08/26/2020 08-26-2020 26-08-2020 26/08/2020
In the above example,
1. The new Date() object gives the current date and time.
let currentDate = new Date();
console.log(currentDate);
// Output
// Wed Aug 26 2020 10:45:25 GMT+0545 (+0545)
2. The getDate() method returns the day from the specified date.
let day = currentDate.getDate();
console.log(day); // 26
3. The getMonth() method returns the month from the specified date.
let month = currentDate.getMonth() + 1;
console.log(month); // 8
4. 1 is added to the getMonth() method because month starts from 0. Hence, January is 0, February is 1, and so on.
5. The getFullYear() returns the year from the specified date.
let year = currentDate.getFullYear();
console.log(year); // 2020
Then you can display the date in different formats.