0

I'm stuck on this activity of mine where we need to subtract the inputs of two dates: It keeps saying "Uncaught TypeError: Cannot read properties of null (reading 'value')" and I'm using CodePen.io as the IDE

HTML:

From Date: 
<input id="start" name="trip-start"> 
<p>
To Date:
<input id="end" name="trip-end"><p>
<button id="Subtract" onclick="document.getElementById('demo').innerText = 
subtract(document.getElementById('Difference_In_Days').value)" > Show Me Difference </button> 
<p>
<input id="demo">

JScript:

function subtract(txt) 
{
var date1 = txt.match(/^\d?\d\/(\d?\d)\/\d{4}$/);
return date1? !!Date.parse(txt) && new Date(txt).getDate()==date1[1] : false;
var date2 = txt.match(/^\d?\d\/(\d?\d)\/\d{4}$/);
return date2? !!Date.parse(txt) && new Date(txt).getDate()==date2[1] : false;

var Difference_In_Time = date2.getTime() - date1.getTime();
var Difference_In_Days = String(Difference_In_Time());
}

1 Answers1

0

const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
const firstDate = new Date(2008, 1, 12);
const secondDate = new Date(2008, 1, 22);

const diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay));

console.log(diffDays)

The question is already answered in

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

and

How to calculate the number of days between two dates?

sojin
  • 2,158
  • 1
  • 10
  • 18