Nicolas Decroos Nicolas Decroos - 1 year ago 221
jQuery Question

New Date() returns invalid date

I'm trying to calculate a difference between 2 days using jQuery. The input fields are the Bootstrap datepicker ones.

When I console.log the field values, they give me a date an in the format dd-mm-yyyy





However when I try to use a new date() (to do the calculations) on it, the variable becomes 'Invalid date'


var start_date = new Date($("#actie_begin").val());


Invalid Date

How can I solve this?

Answer Source

The format you use is not supported by Date.parse.
You could extract the date parts and call the Date(year, month, day) constructor

var starts = $("#actie_begin").val();
var match = /(\d+)\/(\d+)\/(\d+)/.exec(dates)
var start_date = new Date(match[3], match[2], match[1]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download