JavaScript - Display Week Days From A Given Date
Submitted by razormist on Thursday, August 29, 2019 - 16:06.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-4">
- <form>
- <div class="form-group">
- <input type="date" id="date" class="form-control"/>
- <br />
- </div>
- </form>
- </div>
- <div class="col-md-8">
- </div>
- </div>
- </body>
- </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.- function dayInAWeek(date){
- var days = new Array();
- for (var i = 0; i < 7; i++){
- days[i] = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 1 + i);
- }
- return days;
- }
- function getWeekDays(){
- var date = document.getElementById('date');
- if(date.value == ""){
- alert("Please enter a something first!");
- }else{
- var months= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
- var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
- var newDate = new Date(date.value);
- var date_array = new Array();
- date_array = dayInAWeek(newDate);
- var data ="<h2>Week Days</h2>";
- for(var i=0; i < date_array.length; i++){
- data += "<ul><li>"+days[date_array[i].getDay()]+": "+months[date_array[i].getMonth()]+" "+date_array[i].getDate()+", "+date_array[i].getFullYear()+"</li></ul>";
- }
- document.getElementById('result').innerHTML = data;
- }
- }
Add new comment
- 113 views