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:
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 mmm dd, yyyy:
var dateString = "March 20, 2008"; // mmm dd, yyyy
Variable with Date Format as mm dd, yyyy:
var dateString = "Mar 20, 2008"; // mm 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("Year : " + myDate.getFullYear());
Above Example will display that output:
Date :20 Month : 2 Year : 2008
Currently rated 4.9 by 8 people
Tags: javascript, javascript date object, javascript convert string to date, javascript date, javascript time, javascript use, javascript tutorials, learn javascript, javascript examples
8/23/2008 8:41:25 PM