Hello, I am having difficulty coming up with the JAVASCRIPT code and table set u
ID: 3718351 • Letter: H
Question
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.
Here is a link to someone who has answered the same problem I am having, however, I have the same assignment and we are not allowed to use jquery or any help from libraries. Is there a way for this code to work without using jquery? PLEASE help!
https://www.chegg.com/homework-help/questions-and-answers/hello-difficulty-coming-javascript-code-table-set-problem-need-make-webpage-calculates-tim-q27963555?trackid=6e0f561d&strackid=28accd7d&ii=1
Thank you for any help!
Explanation / Answer
//code to copy
<!DOCTYPE html>
<html>
<body>
<h2>Welcome to Mysite</h2>
<form>
Enter Start Date:
<input type="date" id="sdate" name="sdate">
<br><br>
<input id="submit1" type="button" value="Calculate Time!">
</form>
<script>
document.getElementById("submit1").onclick = function() {myFunction()};
function myFunction()
{
var time=_calcTime();
document.write("Time Elapsed since Entered Date: "+time[0]+" Years "+time[1]+" Months "+time[2]+" Days.");
}
function _calcTime() {
var today = new Date()
//Calculates age from given Birth Date in the form//
var givenDate = new Date(today);
var dt1 = document.getElementById("sdate").value;
var startDate = new Date(dt1);
var timeDiff=givenDate.getTime()-startDate.getTime();
var days= Math.ceil(timeDiff / (1000 * 3600 * 24));
var years=Math.floor(days / 365);
var startYear=startDate.getFullYear();
var leapYear=0;
var currYear=givenDate.getFullYear()-1;
if(givenDate.getMonth()>2)
currYear+=1;
for(year=startYear;year<currYear;year++)
{
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
leapYear+=1;
}
}
days=days%365;
days-=leapYear; //leap year - days
var months=Math.floor(days / 31);
days=days%31;
return [years,months,days];
}
</script>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.