JavaScript - Display Week Days From A Given Date

In this tutorial we will create a Display Week Days From A Given Datet using JavaScript. This code will generate a list of days in a week when user provide a input date. The code use onclick() to call a function that display a list of days using for loop to rearrange the index position of arrays to form a group 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 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="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Display Week Days From A Given Date</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-4">
  17. <form>
  18. <div class="form-group">
  19. <label>Enter A Date</label>
  20. <input type="date" id="date" class="form-control"/>
  21. <br />
  22. <center><button type="button" onclick="getWeekDays();" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Submit</button></center>
  23. </div>
  24. </form>
  25. </div>
  26. <div class="col-md-8">
  27. <div id="result"></div>
  28. </div>
  29. </div>
  30. </body>
  31. <script src="js/script.js"></script>
  32. </html>

Creating the Main Function

This code contains the main function of the application. This code will display a list of 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 dayInAWeek(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. }
  6.  
  7. return days;
  8. }
  9.  
  10. function getWeekDays(){
  11. var date = document.getElementById('date');
  12. if(date.value == ""){
  13. alert("Please enter a something 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 date_array = new Array();
  19. date_array = dayInAWeek(newDate);
  20. var data ="<h2>Week Days</h2>";
  21. for(var i=0; i < date_array.length; i++){
  22. data += "<ul><li>"+days[date_array[i].getDay()]+": "+months[date_array[i].getMonth()]+" "+date_array[i].getDate()+", "+date_array[i].getFullYear()+"</li></ul>";
  23. }
  24.  
  25. document.getElementById('result').innerHTML = data;
  26. }
  27. }
There you have it we successfully created a Display Week Days From A Given Date 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