Creating a count down script in javascript

awolarczuk
awolarczuk used Ask the Experts™
on
Hi all i am trying to create a count down to Christ mas that works every year in Java Script i have got current time and date working but for some reason my code isnt  working i am still new to java script :)
function tTc()  //time to christmas
			{
				var cday = new Date("December 25, 2010");   //using as a place holder
				var nextYear = currentDate.GetFullYear();
				var cday.setFullYear(nextYear);
			
			
				var days  = (cday - CurrentDate ) / (1000*60*60*24);
				
				
				alert("There are" +  days + " " days untill Christmas");
				}
				</script>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try this code
<html>
<body>

<script type="text/javascript">
var currentDate = new Date();	
var cday = new Date();   
cday.setMonth(11);
cday.setDate(25);
alert(cday);
var nextYear = currentDate.getFullYear();
cday.setFullYear(nextYear);
			
var days  = (cday - currentDate ) / (1000*60*60*24);
				
				
alert("There are" +  days + " days untill Christmas");
</script>

</body>
</html>

Open in new window

<script>
//The months are from 0 - 11, instead of 1 - 12
var Now   = new Date(2010, 11, 25);
var Xmas1 = new Date(Now.getYear(), 11, 25);
var Xmas2 = new Date(Now.getYear(), 11, 26);
var days = 0;

//Check for Xmas
if ((Now >= Xmas1) && (Now <= Xmas2)) {
   alert("Merry Xmas!");
   }
else {
  //Check if Xmas has allready passed this year. If it has, add another year
  if (Now > Xmas2) {
    Xmas1 = new Date(Now.getYear() + 1, 11, 25);
    Xmas2 = new Date(Now.getYear() + 1, 11, 26);
    }
  days  = Math.round((Xmas1 - Now) / (1000 * 60 * 60 * 24), 0);
  alert("There are " +  days + " days untill Christmas");
  }
</script>

Author

Commented:
guys i have tried both of the codes and they work are of going to work every yea and what it the difference between them apart from the if statment
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

basically @Ironhoofs is checking whether the cristmas is today, already passed or coming in few days.
if latter, then he is calculating the number of days

Author

Commented:
do you both mind if i half the points as i tested them both and they worked
@awolarczuk: my solution just answers your question, but @Ironhoofs solution is better than mine, because it is checking boundary conditions also. I am ok with 150 to me (as assisted) and 250 to him (as accepted)
I am perfectly happy with split points, because @gurvinder372 solution did exactly what was asked for.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial