You can simply use the toLocaleString() method to format a JavaScript date as yyyy-mm-dd.
Let's take a look at the following example to understand how it basically works:
// Create a date object from a date string
var date = new Date("Wed, 04 May 2022");
// Get year, month, and day part from the date
var year = date.toLocaleString("default", { year: "numeric" });
var month = date.toLocaleString("default", { month: "2-digit" });
var day = date.toLocaleString("default", { day: "2-digit" });
// Generate yyyy-mm-dd date string
var formattedDate = day + "-" + month + "-" + year;
console.log(formattedDate); // Prints: 04-05-2022
Use the
toLocaleString()MethodYou can simply use the
toLocaleString()method to format a JavaScript date as yyyy-mm-dd.Let's take a look at the following example to understand how it basically works:
need an explanation for this answer? contact us directly to get an explanation for this answer