WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES
Advertisements

How to Format JavaScript Date as YYYY-MM-DD

Topic: JavaScript / jQueryPrev|Next

Answer: Use the toLocaleString() Method

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

Related FAQ

Here are some more FAQ related to this topic:

Advertisements
Bootstrap UI Design Templates