JavaScript - Display Week Days From A Given Date

Submitted by: 
Visitors have accessed this post 176 times.

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

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="">Sourcecodester</a>
  10. " rel="nofollow">">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 - Display Week Days From A Given Date</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="getWeekDays();" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Submit</button></center>
  24. </div>
  25. </form>
  26. </div>
  27. <div class="col-md-8">
  28. <div id="result"></div>
  29. </div>
  30. </div>
  31. </body>
  32. <script src="js/script.js"></script>
  33. </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. }
  7. return days;
  8. }
  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. }
  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

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.