Javascript - Get The Days of the Week

In this tutorial we will create a Get The Days of the Week using Javascript. 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. This code can be used as your calculator to any mathematical problem. 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.         <head>
  3.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.         </head>
  6.         <nav class="navbar navbar-default">
  7.                 <div class="container-fluid">
  8.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  9.                 </div>
  10.         </nav>
  11.         <div class="col-md-3"></div>
  12.         <div class="col-md-6 well">
  13.                 <h3 class="text-primary">Javascript - Get The Days of the Week</h3>
  14.                 <hr style="border-top:1px dotted #ccc;"/>
  15.                 <div class="col-md-3"></div>
  16.                 <div class="col-md-6">
  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="GetWeek();" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Get Weeks</button></center>
  23.                                 </div>
  24.                                 <div id="result"></div>
  25.                         </form>
  26.                 </div>
  27.         </div>
  28. </body>
  29.  
  30. <script src="js/script.js"></script>
  31. </html>

Creating the Script

This code contains the script of the application. This code will calculate and determine the days of the week. 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 GetWeek(){
  2.         var date = document.getElementById('date');
  3.         if(date.value == ""){
  4.                 alert("Please enter a date first!");
  5.         }else{
  6.                 var months= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  7.                 var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  8.                 var newDate = new Date(date.value);
  9.                 var dates = new Array();
  10.                 dates = CalculateDayOfWeek(newDate);
  11.                 var data ="<h2>Days of the week</h2>";
  12.                 for(var i=0; i < dates.length; i++){
  13.                         data += "<label class='text-success'>"+days[dates[i].getDay()]+": "+months[dates[i].getMonth()]+" "+dates[i].getDate()+", "+dates[i].getFullYear()+"</label><br />";
  14.                 }
  15.                
  16.                 document.getElementById('result').innerHTML = data;
  17.         }
  18. }
  19.  
  20.  
  21. function CalculateDayOfWeek(date){
  22.     var days = new Array();
  23.     for (var i = 0; i < 7; i++){
  24.         days[i] = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 1 + i);
  25.     }
  26.        
  27.     return days;
  28. }
There you have it we successfully created a Get The Days of the 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!

Add new comment