Prashanth Sams Prashanth Sams - 3 months ago 11
Javascript Question

How to convert String mm/dd/yyyy to Date mm/dd/yyyy using JavaScript?

Please find the snippet with issues below

var date = new Date('9/14/2016');
alert((date.getMonth() + 1) + '/' + (date.getDate()+20) + '/' + date.getFullYear());


enter image description here

On adding 20 days from the current date, it should display expected result as shown in the above image. Currently, the inserted data is not parsed as Date format to get the exact output. any work around on this?

Answer

You can't add days by simply adding 20 to the current date.

The following should work:

var date = new Date('9/14/2016');
date.setDate(date.getDate() + 20); // add 20 days
date.setMonth(date.getMonth() +1); // add one month
alert((date.getMonth()) + '/' + (date.getDate()) + '/' + date.getFullYear()); // output