Hi Friends đź‘‹,

Welcome to Infinitbility ❤️!

Today, we are going to learn How to convert date to mm/dd/yyyy in typescript?, here we will use custom method formatDate() method to convert date to mm/dd/yyyy format.

So, first we have to create a function which get date as a parameter and return formated like we want…

Here, we want format date as month/date/year like.

let’s dive in code…

const formatDate = (date: Date) => {
  function pad(s) { return (s < 10) ? '0' + s : s; }
  var d = new Date(date)
  return [pad(d.getMonth()+1), pad(d.getDate()), d.getFullYear()].join('/')
}

console.log(formatDate(new Date()))

Well, when you run above code, you can able to see date converted in mm/dd/yyyy format.

For now, let’s check the output.

Output

TypeScript, convert date to mm/dd/yyyy format example
TypeScript, convert date to mm/dd/yyyy format example

All the best đź‘Ť.