1

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

Code:

console.log($("#actie_begin").val());

Logs:

27/06/2016

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

Code:

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

Logs:

Invalid Date

How can I solve this?

Ashish Ahuja
  • 4,913
  • 10
  • 51
  • 65
Nicolas
  • 4,130
  • 15
  • 47
  • 80

1 Answers1

7

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(starts)
var start_date = new Date(match[3], match[2], match[1]);
Musa
  • 93,746
  • 17
  • 112
  • 129