1 039 views (last 30 days)

unction valid=valid_date(year,month,date)

if nargin==3

valid1=true;

else valid=false;

return

end

v1=[year]; v2=[month]; v3=[date];

if isscalar(v1)==true && isscalar(v2)==true && isscalar(v3)==true

valid2=true;

else valid=false;

return

end

if year>0 && 0<month && month<=12 && 0<date && date<=31

valid3=true;

else valid=false;

return

end

a=year/4; b=year/100; c=year/400;

if rem(year,4)==0 && rem(year,100)~=0

valid4=true;

else valid4=false;

end

if rem(year,100)==0 && rem(year,400)~=0

valid5=true;

else valid5=false;

end

if rem(year,400)==0

valid4=true;

else valid4=false;

end

if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=29) || (month==4||6||9||11 && date<=30) && valid4==true && valid5==false

valid6=true;

else valid6=false;

end

if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=28) || (month==4||6||9||11 && date<=30) && valid5==true && valid4==false

valid7=true;

else

valid7=false;

end

if valid1==true && valid2==true && valid3==true && valid6==true

valid=true;

elseif valid1==true &&valid2==true && valid3==true && valid7==true

valid=true;

else

valid=false;

return

end

why this code is not working for 2018/4/31 and 2003/2/29? and other random dates. but is works for non scalar and random leap years

SANTOSH KAMBLE
on 3 May 2020

Edited: SANTOSH KAMBLE
on 4 May 2020

function [valid]=valid_date(year,month,date)

y=year;m=month;d=date;

if ~isscalar(y)|| ~isscalar(m) || ~isscalar(d)

valid=false;

return

end

if y>=1 && m==1 && d>=1 && d<=31

valid=true;

elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0

valid=true;

elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)

valid=true;

elseif y>=1 && m==3 && d>=1 && d<=31

valid=true;

elseif y>=1 && m==4 && d>=1 && d<=30

valid=true;

elseif y>=1 && m==5 && d>=1 && d<=31

valid=true;

elseif y>=1 && m==6 && d>=1 && d<=30

valid=true;

elseif y>=1 && m==7 && d>=1 && d<=31

valid=true;

elseif y>=1 && m==8 && d>=1 && d<=31

valid=true;

elseif y>=1 && m==9 && d>=1 && d<=30

valid=true;

elseif y>=1 && m==10 && d>=1 && d<=31

valid=true;

elseif y>=1 && m==11 && d>=1 && d<=30

valid=true;

elseif y>=1 && m==12 && d>=1 && d<=31

valid=true;

else

valid=false;

end

end

anil Reddy
on 21 Oct 2020 at 11:42

if i take (0<month<=12) in place of ((12>=month) && (month>0)) why am i getting mistake

Rik
on 21 Oct 2020 at 11:57

Because that is not how Matlab operations work. The explanation provided by mlint explains it:

per isakson
on 29 Jul 2020

Edited: per isakson
on 29 Jul 2020

Would this function pass?

function [ valid, dt ] = valid_date( y, m, d )

% The names of the input arguments, (year,month,day), are they

% mandatory? Are these names chosen to shadow functions in the

% finance toolbox?

try

% datetime is "smart". Doc says: "Each element of DateVector

% should be a positive or negative integer value [...]. If an

% element falls outside the conventional range, datetime adjusts

% both that date vector element and the previous element."

dt = datetime( y, m, d );

% If datetime didn't adjust any element the input is a

% valid date.

% valid = all( [ year(dt)==y, month(dt)==m, day(dt)==d ] );

vec = datevec( dt );

valid = all( [ vec(1)==y, vec(2)==m, vec(3)==d ] );

catch

% datetime throws an exception when not all input values are

% integers

dt = datetime.empty;

valid = false;

end

end

Rik
on 13 Aug 2020

I would hope so. As an instructor I would applaud lateral thinking like this. Just as I would accept code where someone did this:

for n=1%use a loop because it is a requirement

output=sum(data,2);

end

Iccu OUMOUACHA
on 25 May 2020

function valid=valid_date(year, month, day)

if year<=0 || month<=0 || day<=0 || mod( year , 1 )~=0 || mod( month , 1 )~=0 || mod( day , 1 )~=0

valid=false;

return

end

if month<=12 && (ismember(month, [4 6 9 11]) && ismember(day, [1:30]))

valid=true;

elseif month<=12 && (ismember(month, [1 3 5 7 8 10 12]) && ismember(day, 1:31))

valid=true;

elseif month==2 && (mod(year,4)==0 && mod(year,100)~=0 || mod(year,400)==0 && mod(year,100)==0) && ismember(day, 1:29)

valid=true;

elseif month==2 && ismember(day, 1:28)

valid=true;

else

valid=false;

return

end

end

it works in Matlab, when I test a non-scalar inputs, it return false. But here it doesn't work!!!!! ((Assessment 2))

What is the problem?

Rik
on 25 May 2020

Then you should change your code.

If you think this comment is unhelpful: show the code you used to ensure the output is false for non-scalar inputs. That is the only way someone will be able to help you.

Capulus_love
on 11 Aug 2020

Edited: per isakson
on 11 Aug 2020

function x = valid_date(year,month,day)

if nargin == 3

if isscalar(year) && isscalar(month) && isscalar(day)

if month > 0 && month <= 12 && day >= 0 && year > 0

if (month == 1 || 3 || 5 || 7 || 8 || 10 || 12 && day <= 31)...

|| (month == 2 && day <= 29) && (month == 4 || 6 || 9 || 11 && day <= 30)

c0 = mod(year,4)

c1 = mod(year,100)

c2 = mod(year,400)

if (c0 == 0 && c1 ~=0) || (c1 == 0 && c2 ~= 0) || c2 == 0

x = 'true'

else

x = 'false'

end

else

x = 'false'

end

else

x = 'false'

end

else

x = 'false'

end

else

x = 'false'

end

end

why this code not working? :(

Rik
on 11 Aug 2020

Let's try some cases that will likely point us to an issue:

valid_date(2020,2,29) %returns true

valid_date(2021,2,29) %returns false

valid_date(2021,2,28) %returns false

That last one is a problem. Can you follow the flow of your code where it should mark Feb 29 as valid only in leap years, and the rest of Feb as valid every year?

Also, your code returns the value as a char array, not a logical.

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 3 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/469457-write-a-function-called-valid_date-that-takes-three-positive-integer-scalar-inputs-year-month-day#comment_719600

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/469457-write-a-function-called-valid_date-that-takes-three-positive-integer-scalar-inputs-year-month-day#comment_719600

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/469457-write-a-function-called-valid_date-that-takes-three-positive-integer-scalar-inputs-year-month-day#comment_848985

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/469457-write-a-function-called-valid_date-that-takes-three-positive-integer-scalar-inputs-year-month-day#comment_848985

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/469457-write-a-function-called-valid_date-that-takes-three-positive-integer-scalar-inputs-year-month-day#comment_849101

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/469457-write-a-function-called-valid_date-that-takes-three-positive-integer-scalar-inputs-year-month-day#comment_849101

Sign in to comment.