Please I need assistance with this ASAP! Hello, I am having difficulty coming up
ID: 3718375 • Letter: P
Question
Please I need assistance with this ASAP!
Hello, I am having difficulty coming up with the JAVASCRIPT code and table set up for this problem: I need to make a webpage that calculates the time elapsed since a date entered by a user. The page should include a form that allows users to enter a day, month, and year and then calculate and display the elapsed time in years, months, and days. **The program must include code to convert day values in excess of into months, and months in excess of into years.
However, we are not allowed to use jquery or any help from libraries. Is there a super simple way to code this problem? PLEASE help!
Explanation / Answer
Hello,
I attaching the code for your problem. I am commenting tsome line in htmlpart as well as in the js part. The commented part has code if you donot want to use html5(which provides date as a input element attribute).
index.html
<!DOCTYPE html>
<body>
<h1>Enter your date</h1>
<!-- Day: <input type="number" id="day" max="31"><br>
Month: <input type="number" id="month"><br>
Year: <input type="number" id="year"><br> -->
<input type="date" id="date"><br>
<input type="button" value="Calculate">
<script type="text/javascript">
function calc(){
/*var d=document.getElementById("day").value;
var m=document.getElementById("month").value;
var y=document.getElementById("year").value;
var d1 = new Date(m+"/"+d+"/"+y);*/
var date=document.getElementById("date").value;
var d1= new Date(date);
var d2 = new Date(); //gets the current date
var timeDiff = d2.getTime() - d1.getTime(); //finds the difference in seconds
var DaysDiff = timeDiff / (1000 * 3600 * 24); //convert the difference to days
var year=parseInt(DaysDiff/365); //calculates how many years are there
DaysDiff=DaysDiff-(year*365); // deduct the number of days we have converted into years
var month=parseInt(DaysDiff/30.4); //Similarly find months that can be formed.
DaysDiff=parseInt(DaysDiff-(month*30.4)); //Deduct the number of days assigned as months and the remaining are days left
document.write("Days of difference between <br>"+d1+"<br> and <br>"+d2+" is:<br> " +DaysDiff+" Day(s) "+month+" Month(s) "+year+" Year(s)");
}
</script>
</body>
</html>
If you find any difficulty kinly let me know in the comment section I will help you out. Kindly upvote the answer if you liked it.