WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

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 = year + "-" + month + "-" + day;
console.log(formattedDate);  // Prints: 2022-05-04

Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties