# How to write a function to determine if a date entered is valid

18 views (last 30 days)
Elliot Ogbechina on 28 Dec 2019
Commented: Elliot Ogbechina on 31 Dec 2019
I am to Write a function called valid_date that takes three positive integer scalar inputs year, month, day. If these three represent a valid date, return a logical true, otherwise false. The name of the output argument is valid. If any of the inputs is not a positive integer scalar, return false as well. Here is the code i have tried but keeps returning true for all dates, I have editted it a lot and now i just need a fresh eye to point out what i am doing wrong:
NB: I am not to use any built in MatLab function.
function valid = valid_date (year, month, date)
if (nargin==3)
valid=true;
elseif (isinteger(year)) && year>0 && (isscalar(year))
valid=true;
elseif (isinteger(month))&& month>0 && month <=12 && (isscalar(month))
valid=true;
elseif (isinteger(date)) && date > 0 && (isscalar(date))
valid=true;
elseif (month==1||month==3||month==5||month==7||month==8||month==10||month==12)&& date<=31
valid=true;
elseif date <=30
valid=true;
elseif (isinteger(year/4)) && month == 2 && (~(isinteger(year/100))||(isinteger(year/400))) && date <= 29
valid=true;
else
valid = false;
end

Walter Roberson on 31 Dec 2019
TF = isinteger(A) returns logical 1 (true) if A is an array of integer type. Otherwise, it returns logical 0 (false).
Not integer value, integer type
Elliot Ogbechina on 31 Dec 2019
so what can i use in place of isinteger
Stephen Cobeldick on 31 Dec 2019
"so what can i use in place of isinteger"
Take a look at this answer you were given six hours ago:
Does it use isinteger? (hint: no)
What does it use? (hint: mod)

Jalaj Gambhir on 31 Dec 2019
Hi,
You can try the following function and see if it works for you:
function valid = valid_date(year, month, date)
if(nargin ~= 3)
valid = false;
elseif (~(mod(year,1)==0) || (year<0) || ~isscalar(year))
valid = false;
elseif (~(mod(month,1)==0) || (month<=0) || (month>12) || ~isscalar(month))
valid = false;
elseif (~(mod(date,1)==0) || (date<=0) || ~isscalar(date))
valid = false;
elseif( (month==1||month==3||month==5||month==7||month==8||month==10||month==12)&& date>31 )
valid = false;
elseif ((month==4||month==6||month==9||month==11) && date>30)
valid = false;
elseif(~leapyear(year) && month==2 && date>28)
valid=false;
elseif(leapyear(year) && month==2 && date>29)
valid=false;
else
valid = true;
end

Show 1 older comment
Elliot Ogbechina on 31 Dec 2019
Hello, I have worke on it and i am not supposed to use any date function already in matlab, but the leap year still gives me errors for some inputs.
This is my function
function valid = valid_date(year, month, date)
if(nargin ~= 3)
valid = false;
elseif ((~isscalar(year)||(mod(year,1)~=0) || year<0))
valid = false;
elseif ((~isscalar(month))||(mod(month,1)~=0) || (month<=0) || (month>12))
valid = false;
elseif ((~isscalar(date))||(mod(date,1)~=0) || (date<=0))
valid = false;
elseif(any(month==[1:2:7,8:2:12])&& date>31)
valid = false;
elseif (any(month==[4,6,9,11]) && date>30)
valid = false;
elseif(mod(year,4)~=0 && month==2 && date>28)
valid=false;
elseif((mod(year,4)==0) && (mod(year,400)==0 && mod(year,100)==0) && month==2 && date>29)
valid=false;
else
valid = true;
end
it still recognizes 1900 as a leap year
Stephen Cobeldick on 31 Dec 2019
Nice work. Your code is developing nicely.
Leap years are a bit fiddly. One simple approach would be to calculate the date limit number based on the year (i.e. not have a hard-coded 28 or 29), e.g. replace your two leapyear elseif conditions/statements with something like this:
elseif month==2 && date>(28+(mod(year,400)==0 || (mod(year,100)~=0 && mod(year,4)==0)))
Elliot Ogbechina on 31 Dec 2019
Thanks a lot. That was really a nice way to work around it.
This is the final code and everything worked.
function valid = valid_date(year, month, date)
if(nargin ~= 3)
valid = false;
elseif ((~isscalar(year)||(mod(year,1)~=0) || year<0))
valid = false;
elseif ((~isscalar(month))||(mod(month,1)~=0) || (month<=0) || (month>12))
valid = false;
elseif ((~isscalar(date))||(mod(date,1)~=0) || (date<=0))
valid = false;
elseif(any(month==[1:2:7,8:2:12])&& date>31)
valid = false;
elseif (any(month==[4,6,9,11]) && date>30)
valid = false;
elseif month==2 && date>(28+(mod(year,400)==0 || (mod(year,100)~=0 && mod(year,4)==0)))
valid=false;
else
valid = true;
end
Thanks a lot once again.