0

I am having basic issues when trying to get the date from two calender selections and put them into the below calculation.

Anyone have a solution?

Thanks Iain

<body>
  <h1>Project Details</h1>

  <h3>Please enter the project Notice to Proceed (NTP) date: <input     id="NtpDate" type= "date" name="NtpDate"></h3>
  </br>
  <h3>Please enter the Report date: <input type= "date" name="ReportDate">    </h3>

  <script type="text/javascript">
    // I would like to replace this with the <input type = date> tags ...var     NtpDate = new Date("7/11/2004");
     // I would like to replace this with the <input type = date> tags ... var      ReportDate = new Date("12/12/2010"); 

    var timeDiff = Math.abs(ReportDate.getTime() - NtpDate.getTime());
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
     //alert(diffDays);
    document.write("Period =" + diffDays + " (Days from NTP)");
  </script>`
</body>
Iain
  • 9
  • 3

4 Answers4

0

duplicate question How to calculate date difference in javascript

But this is how you get the value of two datepicker. var ReportDate = document.getElementById('ReportDate').value;

Community
  • 1
  • 1
Lierej39
  • 34
  • 2
0

EDIT : Updated fiddle

You could try something like:

<h3>Please enter the project Notice to Proceed (NTP) date: <input id="NtpDate" type= "date" name="NtpDate"></h3>

<h3>Please enter the Report date: <input type= "date" name="ReportDate" id="abc"></h3>

$("#abc").on("change", function () {
  var olddate = new Date($("#NtpDate").val());
  var newdate = new Date($(this).val());
  var timeDiff = Math.abs(newdate.getTime() - olddate.getTime());
  var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
  alert(diffDays);
});

DEMO FIDDLE

Amit Singh
  • 2,267
  • 4
  • 25
  • 50
0

   <body>
  <h1>Project Details</h1>

  <h3>Please enter the project Notice to Proceed (NTP) date: <input     id="NtpDate" type= "date" name="NtpDate"></h3>
  </br>
  <h3>Please enter the Report date: <input id="ReportDate" type= "date" name="ReportDate" onchange="myFunction()">    </h3>

  <script type="text/javascript">

myFunction = function(){

alert(document.getElementById("NtpDate").value +" >> "+document.getElementById("ReportDate").value)
   

 var NtpDate = new Date(document.getElementById("NtpDate").value);
 
var ReportDate = new Date(document.getElementById("ReportDate").value); 

var timeDiff = Math.abs(ReportDate.getTime() - NtpDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
 //alert(diffDays);
alert("Period =" + diffDays + " (Days from NTP)");}
  </script>`
</body>
akgaur
  • 755
  • 1
  • 6
  • 21
  • rather than alert this, I was trying to get it to write to the page, thats why the allert was commented out. Perhaps I should have removed that. It would also be cool to have the answer in days. – Iain Dec 05 '15 at 14:43
0

momentJS is fantastic for date manipulation and formatting. You'll never get it exactly right by rolling your own because of leap days and such.

moment is as simple as:

moment('some date').diff('other date', 'days')

moment.diff()

caleb
  • 126
  • 4