Javascript Convert String to Date
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:
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.
