Question
My code is giving me the wrong days and hours, mins, sec, I need help with my code. Create variables named days, hrs, mins, and
My code is giving me the wrong days and hours, mins, sec, I need help with my code.
Create variables named days, hrs, mins, and secs containing the days, hours, minutes, and seconds until 9 p.m. on the next 4th of July. (Hint: Use the code from the tny_script.js file in the tutorial case as a guide for calculating these variable values.)
Change the text content of the elements with the IDs dLeft, hLeft, mLeft, and sLeft to the values of the days, hrs, mins, and secs variables rounded down to the next lowest integer.
my code:
showClock();
setInterval("showClock()", 100);
function showClock() {
/* Store the current date and time */
var thisDay=new Date();
var localDate=thisDay.toLocaleDateString();
var localTime=thisDay.toLocaleTimeString();
/* Display the current date and time */
document.getElementById("currentTime").innerHTML=""+localDate+" "+localTime+"";
function nextJuly4(currentDate) {
var cYear = currentDate.getFullYear();
var jDate = new Date("July 4, 2018");
jDate.setFullYear(cYear);
if ((jDate - currentDate) < 0) jDate.setFullYear(cYear + 1);
return jDate;
}
var thisDay = new Date();
var j4Date = nextJuly4(thisDay);
j4Date.setHours(21); //using method to set hours
/* Calculate the days until 4th of July */
var days = (j4Date - thisDay)/(1000*60*60*24);
var hrs = (j4Date - thisDay)/(1000*60*60*24);
var mins = (j4Date - thisDay)/(1000*60*60*24);
var secs = (j4Date - thisDay)/(1000*60*60*24);
/* Calculate the hours left in the current day */
var hrs = (days - Math.floor(days))*24;
/* Calculate the minutes and seconds left in the current hour */
var mins = (hrs - Math.floor(hrs))*60;
var secs = (mins - Math.floor(mins))*60;
/* Display the time left until 4th of July */
document.getElementById("dLeft").textContent = Math.floor(days);
document.getElementById("hLeft").textContent = Math.floor(hrs);
document.getElementById("mLeft").textContent = Math.floor(mins);
document.getElementById("sLeft").textContent = Math.floor(secs);
}
Save your changes to the file and then open the tny_july.html file in your browser. Verify that the page shows the date and time of May 19, 2018 at 9:31:27 a.m., and that the countdown clock shows that Summer Party fireworks will begin in 46 days, 11 hours, 28 minutes, and 33 seconds. The countdown clock will not change because the script uses a fixed date and time for the thisDay variable.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started