A string can be converted into a date in JavaScript through the following ways-
- Creating date object using date string:
Example-1:<!DOCTYPE html><html><head><title>Page Title</title></head><body><center><h1 style="color:green">GeeksforGeeks</h1><p>Convert string into date using JavaScript</p><script>//It returns the Day,Month,Date,Year and timevard =newDate("May 1,2019 11:20:00");document.write(d);</script></center></body></html>chevron_rightfilter_noneOutput:
- Getting the string in DD-MM-YY format using suitable methods:
We use certain methods such as:- getDate-It Returns Day of the month(from 1-31)
- getMonth-It returns month number(from 0-11)
- getFullYear-It returns full year(in four digits )
Example-2:
<!DOCTYPE html><html><head><title>Page Title</title></head><body><center><h1 style="color:green">GeeksforGeeks</h1><p>Convert string intodate using JavaScript</p><script>vard =newDate("May 1, 2019 ");document.write(formatDate(d));functionformatDate(date) {varday = date.getDate();if(day < 10) {day ="0"+ day;}varmonth = date.getMonth() + 1;if(month < 10) {month ="0"+ month;}varyear = date.getFullYear();returnday +"/"+ month +"/"+ year;}</script></center></body></html>chevron_rightfilter_noneOutput:
- Using toDateString():
This method returns date portion of Date object in human readable form.
Example-3:<!DOCTYPE html><html><head><title>Page Title</title></head><body><center><h1 style="color:green">GeeksforGeeks</h1><p>Convert string intodate using JavaScript</p><script>vardate =newDate(2019, 5, 3);document.write(date.toDateString());</script></center></body></html>chevron_rightfilter_noneOUTPUT
Output:
Supported Browsers:
- Google Chrome
- Firefox
- Edge
- Opera
- Apple Safari
Recommended Posts:
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- How to convert UTC date time into local date time using JavaScript ?
- How to convert JSON results into a date using JavaScript ?
- How to check the input date is equal to today's date or not using JavaScript ?
- Convert user input string into regular expression using JavaScript
- How to convert a JavaScript date to UTC?
- How to convert milliseconds to date in JavaScript ?
- How to convert date to another timezone in JavaScript ?
- How to convert string into float in JavaScript?
- How to convert long number into abbreviated string in JavaScript ?
- How to convert a plain object into ES6 Map using JavaScript ?
- How to convert string into a number using AngularJS ?
- Convert boolean result into number/integer in JavaScript
- How to convert arguments object into an array in JavaScript ?
- PHP date() format when inserting into datetime in MySQL
- How to get the first and last date of current month using JavaScript ?
- How to remove time from date using JavaScript ?
- How to calculate the date three months prior using JavaScript ?
- How to check a date is valid or not using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


