Convert string to date in Javascript /Jquery

In this article,I will explain how to convert string to date in Javascript/JqueryYou can use the JavaScript Date Object to convert the string to Date in JavaScript.


JavaScript supports the following Date formats :
MM/dd/yyyy
MMMM dd, yyyy
MMM dd, yyyy
MM-dd-yyyy
yyyy/MM/dd

we have to convert a string date to a Javascript Date object.

Convert string to date in Javascript

Example:String to Date Conversion using JavaScript


<!DOCTYPE html>
<html>
<title>Convert string to date in Javascript</title>
<body>
<script type="text/javascript">
function showDate(){
var d=document.getElementById('date').value;
var d1 = Date.parse(d);
var dt=new Date(d1);

alert(dt);
alert("Date : " + dt.getDate());// Date
alert("Month : " + dt.getMonth());// Month
alert("Year : " + dt.getFullYear()); //Year

var x = document.getElementById("result");
x.innerHTML="Date:"+dt+"<br> Date : " + dt.getDate()+" <br> Month : " + dt.getMonth()+" <br> Year : " + dt.getFullYear();

//There are four ways of initiating a date:

//new Date() // current date and time
//new Date(milliseconds) //milliseconds since 1970/01/01
//new Date(dateString)
//new Date(year, month, day, hours, minutes, seconds, milliseconds)

}
</script>

<table>
<tr>
<td> Date:</td><td><input type='text' name='date' id='date'>  </td>
</tr>
<tr>
<td colspan=2 align=center><input value="Convert" type=button onclick='javascript:showDate()'  /></td>
</tr>
</table>
<p id="result"></p>
</body>
</html>


I hope this example will help you to Convert string to date in Javascript