How to Get Previous and Next Days in JavaScript

Introduction

In this tutorial we will create a How to Get Previous and Next Days in JavaScript. This tutorial purpose is to show you to get the next and previous day from the given date. This will tackle all the important functionalities that will allow you to display the next and previous date. I will provide a sample program to show you the actual coding for this tutorial.

This tutorial is very easy to create just follow my instruction and you will do this too. This program can be use to any system if you want to find the next and previous day for a given date. I will give my best to give you the easiest way of creating this program How to Get Previous and Next Days. So let's do the coding

What is JavaScript ?

JavaScript is side scripting language that can build an interactive web-based applications. It can easily manipulate html and css code to change the current attribute of each element and object. The JavaScript is a universal language for web programming because it contains necessary function that needed to understand the concept of web development.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display buttons and date inputs that needed to determine the next and previous day. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  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://www.sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="container-fluid">
  13. <div class="row">
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">How to Get Previous and Next Days in JavaScript</h3>
  17. <hr style="border-top: 1px SOLID #8c8b8b;"/>
  18. <div class="col-md-4">
  19. <h4>Previous Date</h4>
  20. <h2><span class="text-danger" id="previous"></span></h2>
  21. </div>
  22. <div class="col-md-4">
  23. <h4>Current Date</h4>
  24. <input type="date" id="date"/>
  25. <br /><br />
  26. <button onclick="getPrevious();">Previous</button> | <button onclick="getNext();">Next</button>
  27. </div>
  28. <div class="col-md-4">
  29. <h4>Next Date</h4>
  30. <h2><span class="text-info" id="next"></span></h2>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. </body>
  36. <script src="script.js"></script>
  37. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will display the exact next and previous day base on the given date. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function incrementDate(date, days) {
  2. let result = new Date(date);
  3. result.setDate(result.getDate() + days);
  4. return result;
  5. }
  6.  
  7.  
  8. function decrementDate(date, days) {
  9. let result = new Date(date);
  10. result.setDate(result.getDate() - days);
  11. return result;
  12. }
  13.  
  14.  
  15.  
  16. function getNext(){
  17. let date=document.getElementById('date').value;
  18. if(date !=""){
  19. let next=incrementDate(date, 1).toISOString().split('T')[0];
  20. document.getElementById("next").innerHTML=next;
  21. }else{
  22. alert("Please enter date first")
  23. }
  24. }
  25.  
  26.  
  27. function getPrevious(){
  28. let date=document.getElementById('date').value;
  29. if(date!=""){
  30. let prev=decrementDate(date, 1).toISOString().split('T')[0];
  31. document.getElementById("previous").innerHTML=prev;
  32. }else{
  33. alert("Please enter date first");
  34. }
  35. }

In this code we will bind and target the input date attributes in order to get the details date information.

We then create a special method for the next and previous function. This function will get and set the date information by calling a new Date() function. Next we will set the new date with the setDate() function to get the next and previous day by incrementing or decrementing the date value by one

After creating the method we will now call the method with the function called getNext() and getPrevious() that we will bind it in the button onclick function.

Output:

The How to Get Previous and Next Days in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Get Previous and Next Days in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment