As you learn about the Date class in JavaScript, you may not know how to convert a dd/mm/yyyy string to a Date in JavaScript. You can learn how to with the document below.
Convert a dd/mm/yyyy string to a Date in JavaScript
Don’t use Date.parse()
Do not use Date.parse()
, since it only returns the specified date in milliseconds.
Using the Date() constructor
The Date()
constructor will take either a string value or 3 number or string values indicating year, month and day respectively. Though both have some things to keep note of.
Using Date(string value)
This constructor will take in a string value that must follow the ISO 8601 calendar date extended format ("YYYY-MM-DD")
, so it is required that the date string is converted to yyyy-mm-dd
format.
The best way to convert dd/mm/yyyy
format to yyyy-mm-dd
is by using the .split()
function, after that either use .reverse()
then .join("-")
or preferably use destructuring to assign a variable to each value of the date string.
For context:
String.split(str
separator)
: Splits a String by the separator into an array of sub-strings.
Example:
const str = "a b c";
const arr = str.split(" ");
console.log(arr);
Output:
["a","b","c"]
Array.reverse(): Reverses the order of an array.
Example:
const arr = ["a","b","c"];
console.log(arr.reverse());
Output:
["c","b","a"]
Array.join(str seperator)
: Joins every value of an array into a String which is separated by the separator.
Example:
const arr = ["a","b","c"];
const str = arr.join("-");
console.log(str);
Output:
"a-b-c"
Destructuring: Unpacking values of an array or object into distinct variables.
Example:
const [a,b] = ["abc","def"];
console.log(a);
console.log(b);
Output:
"abc"
"def"
Method 1: Using .reverse().join("-")
const date_string = "21/01/2006"; // LMThong's birthday
const date_array = date_string.split("/");
const new_date_string = date_array.reverse().join("-");
const date = new Date(new_date_string);
console.log(date.toDateString());
Output
Sat Jan 21 2006
Method 2: Destructuring
const date_string = "21/01/2006";
const [month,day,year] = date_string.split("/");
const new_date_string = `${year}-${day}-${month}`;
const date = new Date(new_date_string);
console.log(date.toDateString());
Output
Sat Jan 21 2006
Also, note how this uses Template Literals (`)
which allows you to insert JavaScript snippets into strings.
Using Date(str/int year, str/int month, str/int day)
Also, an option, though if you choose to use this, please use the Destructuring method to assign the date values to variables. Additionally, the month argument ranges from 0-11
and not 1-12
, so it’s required to use the parseInt()
function on the month variable.
Code
const date_string = "21/01/2006";
const [day,month,year] = date_string.split("/");
const date = new Date(year, parseInt(month) - 1, day);
console.log(date.toDateString());
Output
Sat Jan 21 2006
Summary
To convert a dd/mm/yyyy string to a Date in JavaScript, you need to use the Date class’ constructor. You can learn more about the Date class here.
Maybe you are interested:
- Get yesterday’s Date formatted as YYYY-MM-DD in JavaScript
- Get the Sunday of the Current Week using JavaScript
- Format a date as yyyymmdd using javascript

Hello, my name is Davis Cole. I love learning and sharing knowledge about programming languages. Some of my strengths are Java, HTML, CSS, JavaScript, C#,… I believe my articles about them will help you a lot.
Programming Languages: Java, HTML, CSS, JavaScript, C#, ASP.NET, SQL, PHP