Javascript Convert String to Date

Updated on 21 Mar 2012,
Published on 20 Mar 2008

To convert the string to Date in JavaScript you can use the Date Object. JavaScript supports the following Date formats that can be used to convert the string to date

  • MM-dd-yyyy
  • yyyy/MM/dd
  • MM/dd/yyyy
  • MMMM dd, yyyy
  • MMM dd, yyyy

JavaScript Data Object Examples:

You can see the live samples and examples of JavaScript Data Object from the following links:

Following examples show the conversion of string to date using JavaScript Date Object:

Variable with Date Format as MM-dd-yyyy

var dateString = "03-20-2008";  
// MM-dd-yyyy

Variable with Date Format as yyyy/MM/dd

var dateString = "2008/03/20";  
// yyyy/MM/dd

Variable with Date Format as MM/dd/yyyy

var dateString = "03/20/2008";  
// MM/dd/yyyy

Variable with Date Format as MMMM dd, yyyy

var dateString = "March 20, 2008";  
// MMMM dd, yyyy

Variable with Date Format as MMM dd, yyyy

var dateString = "Mar 20, 2008";  
// MMM dd, yyyy

You can use any of the above date string formats that can be passed to the javascript Date Object:

var myDate = new Date(dateString);

document.write("Date :" + myDate.getDate());

document.write("<br>");

document.write("Month : " + myDate.getMonth());

document.write("<br>");

document.write("Year : " + myDate.getFullYear());

Output:

You can see the output of above discussed codes from the following link:

Convert String to Date

Continue to next tutorial: Javascript Day of Week Using Array to learn how to get the name of the week day using Javascript Date functions.

12 Responses to "Javascript Convert String to Date"
That's nice, but is there any way to add in the time too? Like convert date and time to a string?
Error !! java.text.ParseException: Unparseable date: \”01-02-2001\” at java.text.DateFormat.parse(Unknown Source)
pjohnraj
var dateString=document.getElementById('ctl00_ContentPlaceHolder1_txtDOB').value; alert("datestring"+dateString); var Ent_dat=new Date(dateString); alert(Ent_dat); it display invalid date please help me
Ezineasp.net
Hi PJohnraj Your code is working fine. Try to use it on button click and validate the date input. It will show error only if date input is incorrect. Good Luck
Sunny
Try with this:
var dateString = "20/02/2008"; // dd/mm/yyyy [IE, FF]
sivakumar p
hi,
i will get dropdown date value 2009-12-23(yyyy-mm-dd) in java script.this value conver to date format mm/dd/yyyy in java script.

Thanks & Regards
Sivakumar .P
faizan
what if the date also has the time string e.g. 2007-08-08 00:00:00.000
Ezineasp.net
Hi Faizan You can use the JavaScript string functions to split the date time string into parts for getting the values. As the date time string provided by you is invalid type for JavaScript date time functions. If you need the complete code urgently then you can ask back!
nayeema
Thanks a lot for this help. It will be much helpful for me. Thanks again.
Bhushan
Thanks !! It is useful for beginners like me!! Regards!! keep Sharing such things!!
Gut6
Hi, In Chrome and Opera it works well but in IE and FF console shows 'Invalid Date'. I pass to them (IE example): '2012-06-14 11:00:00'. I'm currently working on solve that because it's not a big problem. Thanks a lot for post, despite IE and FF it's OK :)
Gut6
Me again ;) simply solution (from stackoverflow) is to use regular expressions: dateString = "2010-08-09 01:02:03"; reggie = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/g; dateArray = reggie.exec(dateString); dateObject = new Date( (+dateArray[1]), (+dateArray[2])-1, // Careful, month starts at 0! (+dateArray[3]), (+dateArray[4]), (+dateArray[5]), (+dateArray[6]) ); From here: http://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in-javascript
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers