I am trying to obtain the current date, but I am struggling to format it in the desired way. Most methods that I have come across use -
as separators, whereas I need them to be separated by /
.
I attempted a manual approach like this:
const today = new Date();
let dd = today.getDate();
let mm = today.getMonth() + 1;
const yyyy = today.getFullYear();
if(dd < 10)
{
dd = '0' + dd;
}
if(mm < 10)
{
mm = '0' + mm;
}
today = yyyy + '/' + mm + '/' + dd;
While this method works, it is messy and leads to type errors since the values are numbers and I am treating them as strings. Additionally, I tried:
const date = new Date().toISOString().split("T")[0];
This approach provides the date in the correct order but with -
separating the values. Is there a solution available that will format the current date using /
as separators?