0

A user suppose to enter the date and based on the date entered the code determines which day of the week was at that specific date?

The code successfully recognize the day of the week and recognize that each month has 31 days, so if I enter February 31, it automatically recognizes as March 3 if the year is simple, and as March 2 if the year is leap! If I enter some gibberish let's say 2019-15-34 it returns as NaN!

I set up conditions for each month that if the month is January maximal number of days should not surpass 31, and if the month is February maximal number of days should not surpass 28 if the year is simple, and so on for each month. After that I set up alert to show the message "Your data is invalid! Please enter valid data!" and set up continue thereafter to return eventual user to reenter the data this time valid date. If date is valid (the month is not suppose to be greater than 12, and date suppose not to be greater than 31) it suppose to show up the message "OK!Your data is valid!" and the code to continue with execution and to get out of if statement!

However it's evident that even when I enter some date that does not make sense let's say 2019-15-15 the code does not return "Your data is invalid! Please enter valid data!", but no matter of what I enter it comes out "OK! Your date is valid!".

What's wrong and why if statement does not recognize date and month format and does not properly execute the code?

Here is the code:

var flag = false;
while(!flag){
    var anydate = prompt('Enter any date in format yyyy-mm-dd', anydate);
    anydate = new Date(anydate);
    var dd = anydate.getDate();
    var mm = (anydate.getMonth()+1);
    var yyyy = anydate.getFullYear();
    if(((mm<1)||(mm>12)||((mm===1)&&(dd>31))||((mm===2)&&(dd>28))||((mm===3)&&(dd>31))|| ((mm===4)&&(dd>30))||((mm===5)&&(dd>31))||((mm===6)&&(dd>30))||((mm===7)&&(dd>31))||((mm===8)&&(dd>31))||((mm===9)&&(dd>30))||((mm===10)&&(dd>31))||((mm===11)&&(mm>30))||((mm===12)&&(dd>31)))){
        alert('Your data is invalid! Enter valid date please!');
        continue;
        flag = false}else{
        alert('OK! You can continue with process!');
        break;
    };
};

var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

console.log(weekday[anydate.getDay()]);
console.log(anydate.getMonth()+1);
console.log(anydate.getDate());
console.log(anydate.getFullYear());
a = anydate
anydate = mm + '-' + dd + '-' + yyyy;
console.log(anydate);
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Ivan Vrzogic
  • 157
  • 1
  • 8
  • `Date` constructor is smarter than you think. It will not create an invalid date. So, if you want to do all dirty work yourself - operate with Strings. – PM 77-1 Dec 29 '20 at 20:08
  • Your problem is here: `anydate = new Date(anydate);`. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Dec 30 '20 at 01:29
  • @PM77-1—the Date constructor certainly will create invalid dates, e.g. `new Date('2020-20-20')` **must** return an invalid date in implementations compliant with ECMA-262. – RobG Dec 30 '20 at 01:31
  • If I enter some invalid date let's say 2020-20-20, the code isn't reporting that the date is invalid, but it jumps over and reports that the date is valid! However at the end it reports "Invalid Data" and yields results "NaN" for the day of the week. – Ivan Vrzogic Dec 30 '20 at 17:48

2 Answers2

0

I suggest that you shouldn't mess with dates - there are a number of good libraries that make it easy like a spring breeze.

In this snippet I used DayJS:

const simpleDate = dayjs('1970-01-01')
const overflowDate = dayjs('1970-15-43')

console.log(simpleDate.format('YYYY-MM-DD'), overflowDate.format('YYYY-MM-DD'))

console.log(simpleDate.format('ddd')) // expected: Thu
console.log(simpleDate.format('dddd')) // expected: Thursday
console.log(simpleDate.format('MMM')) // expected: Jan
console.log(simpleDate.format('MMMM')) // expected: January
console.log(simpleDate.format('YY')) // expected: 70
console.log(simpleDate.format('YYYY')) // expected: 1970
<script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>
muka.gergely
  • 8,063
  • 2
  • 17
  • 34
  • While this might be good advice, it doesn't answer the OP's question: "What's wrong and why if statement does not recognize date and month format and does not properly execute the code?" so really should be a comment. – RobG Dec 30 '20 at 01:29
0

I finally found out the answer, and basically the problem was anydate = new Date(anydate) within the while loop! Instead of executing anydate = new Date(anydate) which would yield date format without ability to divide it, I've partitioned the anydate variable into an array variable that is consisted of three values all of them are string formats. I simply converted those values of string formats into number formats to be able to compare them and put them into if statement! So instead of dividing date format which was not possible I specified that the format of a date variable suppose to be yyyy-mm-dd without defining it as date format variable! It enables if statement to recognize each particular value within the array and compare them and reject improper values, both by numbers and by type. Outside of the while loop I defined anydate variable in date format and I perpetrated following procedure thereafter. The code is as follows:

var flag = false;
while(!flag){
    var anydate = prompt('Enter the date!', anydate);
    let parts = anydate.split('-');
    yyyy = Number(parts[0]);
    mm = Number(parts[1]);
    dd = Number(parts[2]);
if ((yyyy<0)||(mm<1)||(mm>12)||(dd>31)||(typeof(anydate)==='undefined')||(yyyy%1!==0)||(mm%1!==0)||(dd%1!==0)||((mm===2)&&(dd>29))||((mm===4)&&(dd>30))||((mm===6)&&(dd>30))||((mm===9)&&(dd>30))||((mm===11)&&(dd>30))){
    alert('Your date is invalid! Please enter valid date!');
    continue;
    flag = false;
}else{
    alert('OK! You can continue with process!');
    break;
};
}
anydate = new Date(anydate);
var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    console.log(weekday[anydate.getDay()]);
    console.log(anydate.getMonth()+1);
    console.log(anydate.getDate());
    console.log(anydate.getFullYear());
    console.log(anydate);
Ivan Vrzogic
  • 157
  • 1
  • 8