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 :
MMMM dd, yyyy
MMM dd, yyyy

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>
<title>Convert string to date in Javascript</title>
<script type="text/javascript">
function showDate(){
var d=document.getElementById('date').value;
var d1 = Date.parse(d);
var dt=new Date(d1);

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)


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

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