How to Get the Actual Easter Date in JavaScript

How to Get the Actual Easter Date in JavaScript

Introduction

In this tutorial we will create a How to Get the Actual Easter Date in JavaScript. This tutorial purpose is to teach you how to get the actual date for the easter day. This will tackle all the basic function that will display the easter date. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to display some important like the easter day. I will give my best to provide you the easiest way of creating this program Display the Easter Date. So let's do the coding.

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 html forms and button for display easter date. 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" 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-defalt">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Get the Actual Easter Date in JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3">
  17. <form>
  18. <div class="form-group">
  19. <label>Enter a year</label>
  20. <input type="number" id="year" class="form-control"/>
  21. </div>
  22. <center><button type="button" onclick="displayDate();" class="btn btn-primary">Get Date</button></center>
  23. </form>
  24. </div>
  25. <div class="col-md-6">
  26. <div id="result"></div>
  27. </div>
  28. </div>
  29. <script src="script.js"></script>
  30. </body>
  31. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will try you get the actual easter date base on the entered year. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let m=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2.  
  3.  
  4. function displayDate(){
  5. let year=document.getElementById('year').value;
  6.  
  7. if(year.length == 0){
  8. alert("Please enter something");
  9. }else{
  10. let month=getEasterDate(year)[0];
  11. let day=getEasterDate(year)[1];
  12. document.getElementById('result').innerHTML="<h3>The easter date is: </h3><center><h2 class='text-primary'>"+m[month-1]+" "+day+", "+year+"</h2></center>";
  13. }
  14. }
  15.  
  16. function getEasterDate(year) {
  17. let f = Math.floor;
  18. let G = year % 19;
  19. let C = f(year / 100);
  20. let H = (C - f(C / 4) - f((8 * C + 13)/25) + 19 * G + 15) % 30;
  21. let I = H - f(H/28) * (1 - f(29/(H + 1)) * f((21-G)/11));
  22. let J = (year + f(year / 4) + I + 2 - C + f(C / 4)) % 7;
  23. let L = I - J;
  24. let month = 3 + f((L + 40)/44);
  25. let day = L + 28 - 31 * f(month / 4);
  26.  
  27. return [month,day];
  28. }

In the code above we a method that will calculate the date for the easter day, we will call this getEasterDate(). This function contains several algorithm that will calculate and target the actual easter date. Next method will be called displayDate(), this function purpose is to display the easter date after the calculation process is done.

Output:

The How to Get the Actual Easter Date 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 the Actual Easter Date 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