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));
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?

  • 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? – Ben Segarra 2 days ago
  • 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);. – Ganesh Sanap 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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