Javascript get current week start and end date

Javascript get current week start and end date
Share it:


Display Days In A Week Using JavaScript


Introduction


Here we will learn How To  Display Days In A Week using JavaScript.This code will display a day in a week when the user submits 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 systemThe code use onclick() function to call a specific function that dynamically displays a table of multiplication using a nested for() loops in order to loop through number by multiplying as counting numbers. Feel free to modify and apply it in your system, this is a user-friendly kind of program We will be using JavaScript as a server-side scripting language because It gives greater control of your web page and extends its capability in a modern way approach. It is written in HTML or as external sourcing to add some necessary features to your website.

Getting started:

First, you have to download the bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.


Display Days In A Week Using JavaScript

Description:

This code contains the interface of the application. To create this just write these blocks of code inside the text editor and save it as index.html.
To do this just copy and write these blocks of codes as shown below inside the text editor then save it as script.js inside the js folder.

  1. function calculateWeek(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 getDays(){ var date = document.getElementById('date'); if(date.value == ""){ alert("Please enter a date 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 dates = new Array(); dates = calculateWeek(newDate); var data ="
  2. <h2 class="alert-info">Weeks of "+date.value+"</h2>
  3. "; for(var i=0; i &lt; dates.length; i++){ data += "<label>"+days[dates[i].getDay()]+": <span class="text-info">"+months[dates[i].getMonth()]+" "+dates[i].getDate()+", "+dates[i].getFullYear()+"</span></label><br />
  4. "; } document.getElementById('result').innerHTML = data; } }
     
Output:

When we run the above code we will get a result like as shown below 
Javascript get current week start and end date
Share it:

javascript

Post A Comment:

0 comments: