0

I'm reading the value of the date input and need to workout if there is 42 days or more between the specified date and today's date. This is what I've tried so far:

var sdate = new Date($("#holiday-editor input[name=StartDate]").val()); //This is returing date in the string format
var priorDate = new Date().setDate(sdate - 42).toString(); // This is returning some abstract int value
var dateNow = new Date().getDate().toString(); // this is returning 5 even though I'd like to get today's date in the string format
if (dateNow > priorDate) {
    $("#HolidayBookedLate").show();
}
koninos
  • 4,969
  • 5
  • 28
  • 47
threesixnine
  • 1,733
  • 7
  • 30
  • 56
  • There are many duplicates, [*pick one*](http://stackoverflow.com/search?q=%5Bjavascript%5D+difference+in+days+between+dates). – RobG May 05 '16 at 09:48

1 Answers1

0

If you manipulate dates a lot in your app I'd suggest to use moment.js. It's only 15kb but it has lots of useful features to work with dates.

In your case you can use diff function to get amount of days between two dates.

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
Dmitriy Nevzorov
  • 6,042
  • 1
  • 20
  • 28