Skip to main content

Display Days In A Week Using JavaScript

In this tutorial we will create a Display Days In A Week using JavaScript. This code will display a days in a week when user submit the form input. The code use onclick() to call a function that will display a list of arrays that consist of days using for loop to loop through the index position of arrays in a form of weekdays. This a free program feel free to modify it and use it in your system.

JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented.

Getting started:

This is the link for the bootstrap that has been used for the layout of the calculator

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>
  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. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Display Days Of A Week Using JavaScript Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form>
  19. <div class="form-group">
  20. <label>Enter A Date</label>
  21. <input type="date" id="date" class="form-control"/>
  22. <br />
  23. <center><button type="button" onclick="getDays();" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Get Weeks</button></center>
  24. </div>
  26. </form>
  27. </div>
  28. <div class="col-md-8">
  29. <div id="result"></div>
  30. </div>
  31. </div>
  32. </body>
  34. <script src="js/script.js"></script>
  35. </html>

Creating the Main Function

This code contains the main function of the application. This code will display a days in a week when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor then save it as script.js inside the js folder.

  1. function calculateWeek(date){
  2. var days = new Array();
  3. for (var i = 0; i < 7; i++){
  4. days[i] = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 1 + i);
  5. }
  7. return days;
  8. }
  10. function getDays(){
  11. var date = document.getElementById('date');
  12. if(date.value == ""){
  13. alert("Please enter a date first!");
  14. }else{
  15. var months= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  16. var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  17. var newDate = new Date(date.value);
  18. var dates = new Array();
  19. dates = calculateWeek(newDate);
  20. var data ="<h2 class='alert-info'>Weeks of "+date.value+"</h2>";
  21. for(var i=0; i < dates.length; i++){
  22. data += "<label>"+days[dates[i].getDay()]+": <span class='text-info'>"+months[dates[i].getMonth()]+" "+dates[i].getDate()+", "+dates[i].getFullYear()+"</span></label><br />";
  23. }
  25. document.getElementById('result').innerHTML = data;
  26. }
  27. }

There you have it we successfully created a Display Days In A Week 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!


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.