So I'm trying to verify a date of bith with the following code and I am having trouble implementing the if statements that check to see if month
is greater than 12, it should throw an error and if day
is greater than 31 it should also do the same. I would highly appreciate your help.
function isYearCorrect(string) {
let dateOfBirth = new Date((string.substring(0, 2)), (string.substring(2, 4) - 1), (string.substring(4, 6)))
let year = dateOfBirth.getFullYear();
let month = dateOfBirth.getMonth() 1;
let day = dateOfBirth.getDate();
let isDOBValid = false;
if (month < 10) {
month = "0" month;
}
/**This statement does not check to see if the month ranges from 1 - 12 but it skips
to the next year and month and leaves the day as is. Basically 971315 becomes 1998-01-15*/
if (month > 12) {
return (`${month} is an invalid month`)
} else {
month;
}
if (day < 10) {
day = "0" day;
} if (day > 31) {
return (`${day} is an invalid month`);
}else {
day;
}
let fullDate = `${year}-${month}-${day}`;
let dateRegex = /^([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$/;
if (dateRegex.test(fullDate) == false) {
isDOBValid;
} else if (dateRegex.test(fullDate) == true) {
isDOBValid = true;
}
return isDOBValid;
}
console.log(isYearCorrect("970812"))
console.log(isYearCorrect("721329"))
CodePudding user response:
You need to put your validation checks conditions before converting the string
to the date.
function isYearCorrect(string) {
let yearPiece = string.substring(0, 2);
let monthPiece = string.substring(2, 4);
let dayPiece = string.substring(4, 6);
if (monthPiece > 12) {
return (`${monthPiece} is an invalid month`)
}
if (dayPiece > 31) {
return (`${dayPiece} is an invalid month`);
}
let dateOfBirth = new Date(yearPiece, monthPiece, dayPiece)
let year = dateOfBirth.getFullYear();
let month = dateOfBirth.getMonth() 1;
let day = dateOfBirth.getDate();
let isDOBValid = false;
/**This statement does not check to see if the month ranges from 1 - 12 but it skips
to the next year and month and leaves the day as is. Basically 971315 becomes 1998-01-15*/
if (month < 10) {
month = "0" monthPiece;
}
if (day < 10) {
day = "0" day;
}
let fullDate = `${year}-${month}-${day}`;
let dateRegex = /^([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$/;
if (dateRegex.test(fullDate)) {
isDOBValid = true;
}
return isDOBValid;
}
console.log(isYearCorrect("970812"))
console.log(isYearCorrect("721329"))
CodePudding user response:
TLDR: It works as designed.
The solution is to parse the string before putting this into new Date constructor, because what you are trying to do is somehow already done by Date object.
What is happening is that Date is "clever" and it prevents from invalid dates and triggers inner mechanisms, so that when you for example call this function with value "721429" which is supposed to be invalid, you get as a result "1973-3-1". Which is correct (keep in mind February is shorter).
Here you can read more about a date itself and it's acceptable constructors: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
What is more - I suggest not to use "string" as a name of variable and use const where it's possible (e.g. regex or fullDate variable).