There are some ways to format a date as yyyy-mm-dd in javascript. Let’s learn about it with the explanation and examples below.
Format a date as yyyy-mm-dd in javascript
Method 1: Using the toLocaleString() Method
The toLocaleString() method returns a date object like locale settings.
Syntax:
Date.toLocaleString(locales, options)
Parameters:
- locales: Optional. What language format to use? Ex: Australian English is en-Au, Canada English is en-CA.
- options: Optional. You can set some properties in the object.
Return Value: The return value is a String.
Example:
// Create a date object from a date string
var d = new Date();
// en-CA is the Legal value of Canada English
var day = d.toLocaleString("en-CA", { day: "2-digit" });
var month = d.toLocaleString("en-CA", { month: "2-digit" });
var year = d.toLocaleString("en-CA", { year: "numeric" });
// expected output is 2022-09-16 (today)
console.log(year + "-" + month + "-" + day);
Output
2022-09-16
Method 2: Using toISOString method
The toISOString() method returns a date object as a string, using the ISO standard.
Syntax:
Date.toISOString()
Parameters: None
Return Value: The return value is a String, representing the date using the ISO standard format.
Example:
const formatDate = (date) => {
//Return to Legal Value : Canada English
return new Date(date).toLocaleDateString('en-CA')
}
console.log(formatDate('September 16, 2022'));
Output
2022-09-16
Method 3: Creating the Function to format the day
Example:
//create a function to format day as YYYY-MM-DD
const Format = (today) => {
let date = new Date(today);
//use method getDate() , getMonth, getFullYear
let dd = date.getDate().toString();
let mm = (date.getMonth() + 1).toString();
let yyyy= date.getFullYear();
//we will insert 0 and month if the month is less than 10 (mean length <2)
if (mm.length < 2) {
mm = '0' + mm;
}
if (dd.length < 2) {
dd = '0' + dd;
}
return [yyyy, mm, dd].join('-');
}
console.log(Format('September 15, 2022'));
//it will work if you insert the days in the week
console.log(Format('Sun September 11, 2022'));
// or you can define a variable to get the current day
let today = new Date(); // the current day is September 16, 2022
console.log(Format(today));
Output
2022-09-11
2022-09-15
2022-09-16
Summary
In this tutorial, we have explained how to format a date as yyyy-mm-dd in javascript using the toISOString() method and the toLocaleString() method. We always hope this tutorial is helpful to you. Leave your comment here if you have questions about Javascript dates. Thanks for your reading.
Maybe you are interested:
- Initialize a date with time zone using javascript
- Get the current year using javascript
- Convert a date to another time zone using javascript

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python