Home  »  CodeProgrammingSnippetsTechnology   »   How to Use Vanilla JavaScript to Format a Date

How to Use Vanilla JavaScript to Format a Date

Posted: August 3, 2022 | by Michael Bright

In this example snippet we will vanilla JavaScript to format a date in this format dd/mm/yyyy.

var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = dd + '/' + mm + '/' + yyyy;

console.log(today);

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.