-1

I thought

<input type="date">

would return Day-Month-Year. I am attempting to calculate the difference in days from the above input. Am I overthinking this?

function combinedDate() {
  (getDate(), getMonth(), getFullYear())
};

function totalDaysUpdateTotals() {
  var difference_In_Days = fday.combinedDate() - sday.combinedDate();
  let lsatotal = difference_In_Days.value * tlsa.value;
  dlsa.innerHTML = `Total LSA Days${difference_In_Days}`;
  tlsa.innerHTML = `Total LSA £${lsatotal}`;
}
<div id="lsacalc">
  <label for="sday"> First Day of Entitlement </label>
  <input type="date" id="sday" name="sday">
  <br><hr>
  <label for="fday"> Last Day of Entitlement </label>
  <input type="date" id="fdayf" name="fday">
  <br><hr>
  <label for="rlsa"> LSA Level </label>
  <input type="text" id="rlsa" name="rlsa" Value="1">
   <br>
  <hr>
  <button onclick="totalDaysUpdateTotals;">Submit</button>
  <br>
  <hr>
  <div class="dlsa" id="tlsa"></div>
  <div class="tlsa" id="tlsa"></div>
  </form>
sbolel
  • 3,486
  • 28
  • 45
loid
  • 27
  • 4
  • Those functions are methods of a `Date` object, they're not top-level functions. – Barmar Jun 12 '20 at 17:05
  • Is your `
    ` missing its opening tag, or is it just left out of the snippet?
    – sbolel Jun 12 '20 at 17:33
  • just left out the snippet as I used document.forms – loid Jun 12 '20 at 20:27
  • See [*How do I get the number of days between two dates in JavaScript?*](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript?r=SearchResults&s=1|367.2367) – RobG Jun 12 '20 at 20:36

1 Answers1

1

You can't combine multiple methods like that.

If you want to calculate the difference in days, get the difference in milliseconds by calling the getTime() method, then divide by the number of milliseconds in a day (86400000).

You also don't use .value to access a variable's value, that's just for getting the value of an input element.

function totalDaysUpdateTotals() {
  var fday = new Date(document.getElementById("fday").value);
  var sday = new Date(document.getElementById("sday").value);
  var difference_In_Days = Math.floor((fday.getTime() - sday.getTime())/86400000);
  let lsatotal = difference_In_Days * tlsa.value;
  dlsa.innerHTML = `Total LSA Days${difference_In_Days}`;
  tlsa.innerHTML = `Total LSA £${lsatotal}`;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612