JavaScript - Simple New Year Countdown

Submitted by: 
Language: 
Visitors have accessed this post 247 times.

In this tutorial we will create a Simple New Year Countdown using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="<a href="https://sourcecodester">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Javascript - Simple New Year Countdown</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button onclick="startCountDown()" type="button" class="btn btn-sm btn-success">Start Countdown</button>
  18. <center><h1 id="title">NEW YEAR COUNTDOWN</h1></center>
  19. <table class="table">
  20. <tr style="background-color:#000;">
  21. <td style="color:#fff;" id="days"><center><h1>0</h1></center></td>
  22. <td style="color:#fff;" id="hrs"><center><h1>00</h1></center></td>
  23. <td style="color:#fff;" id="min"><center><h1>00</h1></center></td>
  24. <td style="color:#fff;" id="sec"><center><h1>00</h1></center></td>
  25. </tr>
  26. <tr>
  27. <td><center><h2>Days</h2></center></td>
  28. <td><center><h2>Hours</h2></center></td>
  29. <td><center><h2>Minutes</h2></center></td>
  30. <td><center><h2>Seconds</h2></center></td>
  31. </tr>
  32. </table>
  33. </div>
  34. <script src="js/script.js"></script>
  35. </body>
  36. </html>

Creating the Script

This code contains the script of the application. This code will start the countdown before the new year eve when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.

  1. function startCountDown(){
  2. var today = new Date();
  3. var target = new Date("January 1, 2019 00:00:00");
  4. var currentTime = today.getTime();
  5. var targetTime = target.getTime();
  6.  
  7. var time = targetTime - currentTime;
  8.  
  9. var sec = Math.floor(time/1000);
  10. var min = Math.floor(sec/60);
  11. var hrs = Math.floor(min/60);
  12. var days = Math.floor(hrs/24);
  13.  
  14. hrs = hrs % 24;
  15. min = min % 60;
  16. sec = sec % 60;
  17.  
  18.  
  19. hrs = (hrs<10) ? "0"+hrs : hrs;
  20. min = (min<10) ? "0"+min : min;
  21. sec = (sec<10) ? "0"+sec : sec;
  22.  
  23. document.getElementById('days').innerHTML = "<center><h1>"+days+"</h1></center>";
  24. document.getElementById('hrs').innerHTML = "<center><h1>"+hrs+"</h1></center>";
  25. document.getElementById('min').innerHTML = "<center><h1>"+min+"</h1></center>";
  26. document.getElementById('sec').innerHTML = "<center><h1>"+sec+"</h1></center>";
  27.  
  28. if(time > 0){
  29. setTimeout(startCountDown, 1000);
  30. }else{
  31. endCountDown();
  32. }
  33. }
  34.  
  35. function endCountDown(){
  36. document.getElementById('title').innerHTML = "HAPPY NEW YEAR!";
  37. document.getElementById('days').innerHTML = "<center><h1>00</h1></center>";
  38. document.getElementById('hrs').innerHTML = "<center><h1>00</h1></center>";
  39. document.getElementById('min').innerHTML = "<center><h1>00</h1></center>";
  40. document.getElementById('sec').innerHTML = "<center><h1>00</h1></center>";
  41. }

There you have it we successfully created a Simple New Year Countdown using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.