Solved

Javascript countdown timer

Posted on 2009-03-29
3
277 Views
Last Modified: 2012-05-06
I'm trying to create a countdown timer in javascript for a web page.  Everything appears to be working correctly except for the seconds. I think it must be my calculation, but I don't know what else to try. Can  anyone see where I'm going wrong?
function calculate()
{
	//load dates
	var startDate = new Date()
	var endDate = new Date("May 19, 2009, 21:00:00")
	
	//load variable
	var secPerYear = 60*60*24*365;
	var secPerDay = 60*60*24;
	var secPerHour = 60*60;
	var secPerMin = 60;
	
	//seconds left right now
	var seconds = (endDate.getTime() - startDate.getTime()) / 1000
	
	//get years left value
	var years = Math.floor(seconds / secPerYear)
	
	//remove seconds from year
	seconds = seconds - (years * secPerYear)
	
	//get day value
	var days = Math.floor(seconds / secPerDay)
	
	//remove seconds from day
	seconds = seconds - (days * secPerDay)
	
	//get hours value
	var hours = Math.floor(seconds / secPerHour)
	
	//remove seconds from hours
	seconds = seconds - (hours * secPerHour)
	
	//get minutes
	var minutes = Math.floor(seconds / secPerMin)
	
	//remove seconds from minutes
	seconds = seconds - (seconds * secPerMin)
		
	seconds = (seconds / 1000);
	
		
	lblYears.innerHTML = years
	lblDays.innerHTML = days
	lblHours.innerHTML = hours
	lblMinutes.innerHTML = minutes
	lblSeconds.innerHTML = seconds

Open in new window

0
Comment
Question by:98fatboyrider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 31

Assisted Solution

by:Frosty555
Frosty555 earned 200 total points
ID: 24013712
Everything looks fairly sound, but I think your last line:

seconds = (seconds / 1000);

Isn't necessary. Your seconds are already in seconds, you shouldn't need to divide by 1000 again.
0
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 300 total points
ID: 24019503
you can try this, much more simplified
<script type="text/javascript">

        var startDate = new Date()
        var endDate = new Date("May 1, 2009, 22:50:00")
        var differentDate;
        differentDate = new  Date(endDate - startDate);
        document.write("differentDate.getSeconds() : " + differentDate.getSeconds() + "<br/>" );
        document.write("differentDate.getMinutes() : " + differentDate.getMinutes() + "<br/>" );      
        document.write("differentDate.getHours() : " + differentDate.getHours() + "<br/>" );      
        document.write("differentDate.getDate() : " + differentDate.getDate() + "<br/>" );      
        document.write("differentDate.getMonth() : " + (differentDate.getMonth()-1) + "<br/>" );      
        document.write("differentDate.getFullYear() : " + differentDate.getFullYear() + "<br/>" );      
</script>
0
 

Accepted Solution

by:
98fatboyrider earned 0 total points
ID: 24068334
Turns out it was a calculation error at line 38. I should have been multiplying by minutes, not seconds. Once that was working, I rounded the result to trim the milliseconds. Thanks for the help.
function calculate()
{
	//load dates
	var startDate = new Date()
	var endDate = new Date("May 12, 2009, 21:00:00")
	
	//load variables
	var secPerYear = 60*60*24*365;
	var secPerDay = 60*60*24;
	var secPerHour = 60*60;
	var secPerMin = 60;
	
	//seconds left right now
	var seconds = (endDate.getTime() - startDate.getTime()) / 1000
	
	//get years left
	var years = Math.floor(seconds / secPerYear)
	
	//remove seconds from year
	seconds = seconds - (years * secPerYear)
	
	//get days
	var days = Math.floor(seconds / secPerDay)
	
	//remove seconds from day
	seconds = seconds - (days * secPerDay)
	
	//get hours value
	var hours = Math.floor(seconds / secPerHour)
	
	//remove seconds from hours
	seconds = seconds - (hours * secPerHour)
	
	//get minutes
	var minutes = Math.floor(seconds / secPerMin)
	
	//remove seconds from minutes
	seconds = seconds - (minutes * secPerMin)
	
	seconds = Math.round(seconds) -1
	
	lblYears.innerHTML = years
	lblDays.innerHTML = days
	lblHours.innerHTML = hours
	lblMinutes.innerHTML = minutes
	lblSeconds.innerHTML = seconds
}

Open in new window

0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question