lundi 12 août 2019

Display the Results of an If Statement

I have an if statement in my code that I can't seem to get to display. I keep getting undefined(output3). This is a calculation to determine the days until next birthdate. Basically if the birth date has already passed, it adds one year to the birthdate and subtracts the current date. How would I display this in the statement at top of JS code along with the other statements that display when the Calculate button is pressed? Thx!

This is the main block of code that's giving me fits on how to display it.

if (diff.cbdiff < 0 ) {

            var date = new Date(currentBday); 
            date.setFullYear(date.getFullYear() + 1);
            document.getElementById('output3').innerHTML= ((date-date2)/(1000*60*60*24));
            console.log(output3);
        } else {
            document.getElementById('output3').innerHTML=diff.cbdiff;
             console.log(output3);
        }

Here is complete JS code:

function calc() {
  var date1 = new Date();
  var date2 = new Date(document.getElementById("date2").value);
  var diff = dateDiff(date1, date2);
  var output = "You are " +diff.year + " year" +"s "+ diff.month + " month" +"s And " + diff.day + " day" +"s Old";
  var output1 = "You are " +diff.days +" day" +"S" +"old";
  var output2 = "You are " +diff.secondsalive +" seconds" +"old";
  var output3 = "It is " +output3+ " days until your next Birthday";

  document.getElementById("output").innerHTML = output;
  document.getElementById("output1").innerHTML = output1;
  document.getElementById("output2").innerHTML = output2;
  document.getElementById("output3").innerHTML = output3;


  console.log(date1);
  console.log(date2);

}

function dateDiff(date1, date2) {
  if(date1>date2) return dateDiff(date2, date1);
  var diff = {}; 
  //console.log(currentBday);

  diff.secondsalive = ((date2 - date1)/(1000)).toLocaleString({undefined,maximumFractionDigits:1});
  diff.day = date2.getDate() - date1.getDate();
  diff.days = ((date2 - date1)/(1000*60*60*24)).toFixed(1);
  diff.year = date2.getFullYear() - date1.getFullYear();
  diff.month = date2.getMonth() - date1.getMonth();

  var currentBdYear = date2.getFullYear();
  var currentBdMonth=date1.getMonth()+1;
  var currentBdDay =date1.getDate();
  var currentYearBD = (diff.year + date2);

var addDate = new Date(diff.year); 
//console.log(addDate);


  var currentBday=new Date(currentBdMonth+"/"+currentBdDay+"/"+currentBdYear);
  diff.cbdiff =((currentBday-date2)/(1000*60*60*24)).toFixed(0);
  console.log(diff.cbdiff);



if (diff.cbdiff < 0 ) {

            var date = new Date(currentBday); 
            date.setFullYear(date.getFullYear() + 1);
            document.getElementById('output3').innerHTML= ((date-date2)/(1000*60*60*24));
            console.log(output3);
        } else {
            document.getElementById('output3').innerHTML=diff.cbdiff;
             console.log(output3);
        }



if (diff.day < 0) {
    diff.month--;
    var dayDiff = new Date(date2.getYear(), date2.getMonth(), 0).getDate() - date1.getDate();
    diff.day = date2.getDate();
    if (dayDiff > 0) {
      diff.day += dayDiff;
    }
  }
  if (diff.month < 0) {
    diff.month += 12;
    diff.year--;
  }

  return diff;
}

HTML

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body id ="gradient">



    <div>
    <h1>Birthday Calculator</h1>
    <h2> Enter Birthday</h2>



  <td>BirthDate:</td>
  <input id="date2" value ="10/1/74" />
    <button onclick="calc()" style="height:50px">Calculate</button>
    <br><br>

<table class = "center">

<tr>
  <td><div id="output"></div></td>
</tr>
<tr>
  <td><div id="output1"></div></td>
</tr>
<tr>
  <td><div id="output2"></div></td>
</tr>
<tr>
  <td><div id ="output3"></div></td>
</tr>
</table>

<p It is id ="output3"></p>

    <h3></h3>
    <script type="text/javascript" src ="script.js"></script>

</body>
</html>

Aucun commentaire:

Enregistrer un commentaire