79496724

Date: 2025-03-09 22:51:07
Score: 1.5
Natty:
Report link

If every date is going to be in the format yyyyMMdd and your use case is simple, then you can divide the string up into these segments and assign them to variables.

let monthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function checkDate(date) {
    if (date.length === 8) {
        let year = parseInt(date.slice(0, 4));
        let month = parseInt(date.slice(4, 6));
        let day = parseInt(date.slice(6, 8));
        if (month > 12 || month < 0) {console.log("Invalid month"); return 0;}
        if (day > monthDays[month - 1]  || day < 0 || (month === 2 && day >= 28 && year % 4 != 0)) {console.log("Invalid day"); return 0;}
        return `${year}, ${month}, ${day}`;
    } else {
        console.log("Invalid length");
        return 0;
    }
}

dont downvote guys i am in the process of editing this

Reasons:
  • RegEx Blacklisted phrase (2): downvote
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: oosh