0

I want to get the difference in days between two dates. One date is from a List item input by User. The second day will be "Today". get the number of days from the date input by user and current day.

Will this work?

var onBoardDate = value.OnBoardDate; //value obtained via REST call

var today = new Date(); // current day
var dateToString2 = today;
var dateDiff = dateToString2 - dateToString1;
console.log(dateDiff);

var date_diff_indays = function (date1, date2) {
  dt1 = dateToString1;
  dt2 = dateToString2;
  return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate())) / (1000 * 60 * 60 * 24));
}
console.log(date_diff_indays(dt1, dt2));

1 Answer 1

0

I can see there are some syntax errors in your code like:

  1. dateToString1 is not defined anywhere.
  2. Arguments/Parameters you are passing and using in date_diff_indays() function are different.

Check below question on Stack Overflow to get the number of days between two dates:

How do I get the number of days between two dates in JavaScript?

2
  • The date I retrieve via REST Call from a SharePoint date picker field, what format is it in? Do I need to change the format of the date picker field? Oct 11, 2019 at 11:45
  • It depends on which solution you use. Retrieved date will be in ISO date format. You can create date object from it simply by using var onBoardDate = new Date(value.OnBoardDate);. Oct 11, 2019 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.