Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- function Test() {
- let btn = document.querySelector("button");
- btn.addEventListener("click", () => {
- let year = Number(document.getElementById("year").value);
- let month = document.getElementById("month").value;
- let day = Number(document.getElementById("day").value);
- let outputDiv = document.getElementById("output");
- let p = document.createElement("p");
- function isYearLeapAndValid(year) {
- return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
- }
- function isMonthValid(month) {
- let months = ["January", "February", "March", "Aprim", "May", "June", "July", "August", "September", "October", "November", "December"];
- return (months.includes(month.toString()));
- }
- function isDayValid(day) {
- return day > 0 && day < 32;
- }
- function daysInMonth(m, y) { // m is 0 indexed: 0-11
- switch (m) {
- case "February":
- return (y % 4 == 0 && y % 100) || y % 400 == 0 ? 29 : 28;
- case "September": case "April": case "July": case "10":
- return 30;
- default:
- return 31
- }
- }
- function isInputValid(year, month, day) {
- if (isMonthValid(month) == true && isDayValid(day) == true) {
- if (daysInMonth(month, year) > 0 && daysInMonth(month, year) >= day) {
- return true;
- }
- } else {
- return false
- }
- }
- function format(d) {
- let x = new String(d);
- if (x.length < 2) {
- return d = "0" + d
- }
- return d;
- }
- function getIndexOfMonth(month) {
- let months = ["January", "February", "March", "Aprim", "May", "June", "July", "August", "September", "October", "November", "December"].indexOf(month);
- return months;
- }
- day = format(day);
- if (isInputValid(year, month, day)) {
- month = getIndexOfMonth(month);
- month = format(month);
- p.innerText = `Date: ${year}-${month}-${day} is valid`;
- } else {
- p.innerHTML = `Date: ${year}-${month}-${day} is not valid`;
- }
- document.getElementById("year").value="";
- document.getElementById("month").value="";
- document.getElementById("day").value="";
- outputDiv.appendChild(p);
- });
- }
- return function () {
- Test();
- }
- })();
Add Comment
Please, Sign In to add comment