Guest User

Untitled

a guest
Jan 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. (function () {
  2.  
  3. function Test() {
  4. let btn = document.querySelector("button");
  5.  
  6. btn.addEventListener("click", () => {
  7. let year = Number(document.getElementById("year").value);
  8. let month = document.getElementById("month").value;
  9. let day = Number(document.getElementById("day").value);
  10.  
  11. let outputDiv = document.getElementById("output");
  12. let p = document.createElement("p");
  13.  
  14.  
  15. function isYearLeapAndValid(year) {
  16. return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
  17. }
  18.  
  19. function isMonthValid(month) {
  20. let months = ["January", "February", "March", "Aprim", "May", "June", "July", "August", "September", "October", "November", "December"];
  21. return (months.includes(month.toString()));
  22. }
  23.  
  24. function isDayValid(day) {
  25. return day > 0 && day < 32;
  26. }
  27.  
  28. function daysInMonth(m, y) { // m is 0 indexed: 0-11
  29. switch (m) {
  30. case "February":
  31. return (y % 4 == 0 && y % 100) || y % 400 == 0 ? 29 : 28;
  32. case "September": case "April": case "July": case "10":
  33. return 30;
  34. default:
  35. return 31
  36. }
  37. }
  38.  
  39.  
  40. function isInputValid(year, month, day) {
  41.  
  42.  
  43. if (isMonthValid(month) == true && isDayValid(day) == true) {
  44. if (daysInMonth(month, year) > 0 && daysInMonth(month, year) >= day) {
  45. return true;
  46. }
  47. } else {
  48. return false
  49. }
  50.  
  51. }
  52.  
  53. function format(d) {
  54. let x = new String(d);
  55. if (x.length < 2) {
  56. return d = "0" + d
  57. }
  58. return d;
  59. }
  60.  
  61. function getIndexOfMonth(month) {
  62. let months = ["January", "February", "March", "Aprim", "May", "June", "July", "August", "September", "October", "November", "December"].indexOf(month);
  63. return months;
  64. }
  65.  
  66. day = format(day);
  67.  
  68. if (isInputValid(year, month, day)) {
  69. month = getIndexOfMonth(month);
  70. month = format(month);
  71.  
  72. p.innerText = `Date: ${year}-${month}-${day} is valid`;
  73. } else {
  74. p.innerHTML = `Date: ${year}-${month}-${day} is not valid`;
  75. }
  76. document.getElementById("year").value="";
  77. document.getElementById("month").value="";
  78. document.getElementById("day").value="";
  79.  
  80. outputDiv.appendChild(p);
  81. });
  82. }
  83. return function () {
  84. Test();
  85. }
  86. })();
Add Comment
Please, Sign In to add comment