Skip to main content

Display Easter Date Using JavaScript

Screenshot
Body

In this code we will try to create Display Easter Date using JavaScript. The code will display the actual easter date base on the given year. This a user-friendly kind of code, feel free to modify it and use it as your coding reference. To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, 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. <br />
  2. <!DOCTYPE html><br />
  3. <html lang="en"><br />
  4. <head><br />
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/><br />
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /><br />
  7. </head><br />
  8. <body><br />
  9. <nav class="navbar navbar-defalt"><br />
  10. <div class="container-fluid"><br />
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a><br />
  12. </div><br />
  13. </nav><br />
  14. <div class="col-md-3"></div><br />
  15. <div class="col-md-6 well"><br />
  16. <h3 class="text-primary">Display Easter Date Using JavaScript</h3><br />
  17. <hr style="border-top:1px dotted #ccc;"/><br />
  18. <div class="col-md-3"><br />
  19. <form><br />
  20. <div class="form-group"><br />
  21. <label>Enter a year</label><br />
  22. <input type="number" id="year" class="form-control"/><br />
  23. </div><br />
  24. <center><button type="button" onclick="displayDate();" class="btn btn-primary">Get Easter</button></center><br />
  25. </form><br />
  26. </div><br />
  27. <div class="col-md-6"><br />
  28. <div id="result"></div><br />
  29. </div><br />
  30. </div><br />
  31. <script src="js/script.js"></script><br />
  32. </body><br />
  33. </html><br />

Creating the Script

This code contains the script of the application. The code will dynamically limit the password length when user click the button. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. <br />
  2. var m=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];</p>
  3. <p>function getEasterDate(year) {<br />
  4. var f = Math.floor;<br />
  5. var G = year % 19;<br />
  6. var C = f(year / 100);<br />
  7. var H = (C - f(C / 4) - f((8 * C + 13)/25) + 19 * G + 15) % 30;<br />
  8. var I = H - f(H/28) * (1 - f(29/(H + 1)) * f((21-G)/11));<br />
  9. var J = (year + f(year / 4) + I + 2 - C + f(C / 4)) % 7;<br />
  10. var L = I - J;<br />
  11. var month = 3 + f((L + 40)/44);<br />
  12. var day = L + 28 - 31 * f(month / 4);</p>
  13. <p> return [month,day];<br />
  14. }</p>
  15. <p>function displayDate(){<br />
  16. var year=document.getElementById('year').value;</p>
  17. <p> if(year.length == 0){<br />
  18. alert("Please enter something");<br />
  19. }else{<br />
  20. var month=getEasterDate(year)[0];<br />
  21. var day=getEasterDate(year)[1];<br />
  22. document.getElementById('result').innerHTML="<h4>Easter Date: </h4><center><h2 class='text-primary'>"+m[month-1]+" "+day+", "+year+"</h2></center>";<br />
  23. }<br />
  24. }<br />

There you have it we successfully created a Display Easter 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!

Add new comment