0

i am working on a date test javascript program that will let the user enter a date. It will parse the date to display the Month, day and year on separate lines with appropriate labels. It will also compare the current date with the entered date to display the number of day difference between them. So far I have been able to get the current dat to display and take users input but do not know how to calculate the number of days inbetween against the date I input. For some reason it is using the year 1969 I think to give me number of days difference and can not get it to parse into seperate lines. Have been working on this for weeks and is breaking my head on different ways to do so. So far I have the following:

<header>
    <h1>Date Test</h1>
</header>
<br>
<p>Please enter date:</p>
<input id="inp" type="date">
<br>
<br>
<button type="button" onclick="date_test()">Process</button>
<br>
<p id="iop"></p>
<br>
<p id="op"></p>
<br>
<p id="dd"></p>

<script>

    document.getElementById("op").innerHTML = Date();

        function date_test() {

            var d = document.getElementById("inp").value;
            document.getElementById("iop").innerHTML = d;

            var inpu = document.getElementById("inp").value;
            var da = Date.parse(inpu);
            var minutes = 1000 * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var x = Math.round(da / days);

            document.getElementById("dd").innerHTML = x;

        }
</script>

Ed Villa
  • 69
  • 9
  • 1
    you'll need the universal "difference" operator ... `-` ... suspiciously absent in your code – Jaromanda X Apr 10 '17 at 04:34
  • Please search first, [*\[javascript\] days difference*](http://stackoverflow.com/search?q=[javascript]+days+difference) returns many similar questions. Pick any reasonable one as a duplicate. – RobG Apr 10 '17 at 04:43

4 Answers4

0

Here is a simple function that you can use

function calculate_days(date1, dat2){
    return (date2-date1)/(24*3600*1000);
}

date1 = new Date("4-4-2017");
date2 = new Date("4-8-2017");
console.log(calculate_days(date1, date2));

where date1 and date2 are date objects. 1000 in the denominator is needed as the difference would return number of milliseconds.

Harshit Garg
  • 2,137
  • 21
  • 23
  • You should probably use *Math.round* as not all days are 24hrs long over daylight saving boundaries. – RobG Apr 10 '17 at 05:34
0

Here is sample code

<header>
<h1>Date Test</h1>
</header>
<br>
<p>Please enter date:</p>
<input id="inp" type="date">
<br>
<br>
<button type="button" onclick="date_test()">Process</button>
<br>
<p id="iop"></p>
<br>
<p id="op"></p>
<br>
<p id="dd"></p>

<script>

  document.getElementById("op").innerHTML = Date();

  function date_test() {

    var d = document.getElementById("inp").value;
    document.getElementById("iop").innerHTML = d;

    var inpu = document.getElementById("inp").value;
    var da = Date.parse(inpu);
    //here passing current date & selected date as params
    console.log(daysBetween(new Date(), new Date(da)))

  }


  daysBetween = function( date1, date2 ) {
    console.log(date1);
    console.log(date2);
    //Get 1 day in milliseconds
    var one_day=1000*60*60*24;

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = date2_ms - date1_ms;

    // Convert back to days and return
    return Math.round(difference_ms/one_day); 
  }

</script>
Richardson. M
  • 852
  • 2
  • 17
  • 28
  • Then entire function body can be `return Math.round((date1 - date2) / 8.64e7)`. You should never use *Date.parse* (or the Date constructor) for parsing dates as results are unreliable. – RobG Apr 10 '17 at 06:00
0

var date1 = new Date();
var dd = date1.getDate()+20; // add 20 days
var mm = date1.getMonth()+1; //January is 0!
var yyyy = date1.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
date2 = new Date(yyyy+'/'+mm+'/'+dd);

var timeDiff = Math.abs(date2.getTime() - date1.getTime());
days = Math.ceil(timeDiff / (1000 * 3600 * 24));
console.log("day difference: ", days);
Satendra
  • 6,755
  • 4
  • 26
  • 46
0

i think you should use these code

    var date1 = new Date("7/20/2017");
var date2 = new Date("12/25/2017");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays);
Manish Singh
  • 934
  • 1
  • 12
  • 27