Q:

How to Format JavaScript Date as YYYY-MM-DD

0

How to Format JavaScript Date as YYYY-MM-DD

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Generate a Random Number between Two Number... >>
<< How to Get a Random Item from a JavaScript Array...