Validation of a date in given format

7 views (last 30 days)
donia shane
donia shane on 6 Mar 2019
Commented: Rik on 31 Jan 2022
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. Note that every year that is exactly divisible by 4 is a leap year, except for years that are exactly divisible by 100. However, years that are exactly divisible by 400 are also leap years. For example, the year 1900 was not leap year, but the year 2000 was. Note that your solution must not contain any of the date related built-in MATLAB functions.

Answers (5)

Vinay Ram Gazula
Vinay Ram Gazula on 23 Apr 2020
function valid = valid_date(y,m,d)
if nargin == 3
if ~isscalar(y) || y < 1 || y ~= fix(y)
valid = false;
return
elseif ~isscalar(m) || m < 1 || m ~= fix(m)
valid = false;
return
elseif ~isscalar(d) || d < 1 || d ~= fix(d)
valid = false;
return
end
end
a=y/4;b=y/400;c=y/100;
M1 = [1 3 5 7 8 10 12];
M2 = [4,6,9,11];
F1 = (1:29);
F2 = (1:28);
D1 = (1:31);
D2=(1:30);
if a ~= fix(a) || (b ~= fix(b) && c == fix(c))
if ismember(m,M1) && ismember(d,D1)
valid = true;
elseif ismember(m,M2) && ismember(d,D2)
valid = true;
elseif m==2 && ismember(d,F2)
valid = true;
else
valid = false;
end
elseif a == fix(a) || b == fix(b)
if ismember(m,M1) && ismember(d,D1)
valid = true;
elseif ismember(m,M2) && ismember(d,D2)
valid = true;
elseif m==2 && ismember(d,F1)
valid = true;
else
valid = false;
end
end
end

Shubhadeep Sadhukhan
Shubhadeep Sadhukhan on 16 May 2020
Edited: Walter Roberson on 16 May 2020
function valid = valid_date(y,m,d)
if nargin == 3
if ~isscalar(y) || y < 1 || y ~= fix(y)
valid = false;
return
elseif ~isscalar(m) || m < 1 || m ~= fix(m)
valid = false;
return
elseif ~isscalar(d) || d < 1 || d ~= fix(d)
valid = false;
return
end
end
a=y/4;b=y/400;c=y/100;
M1 = [1 3 5 7 8 10 12];
M2 = [4,6,9,11];
F1 = (1:29);
F2 = (1:28);
D1 = (1:31);
D2=(1:30);
if a ~= fix(a) || (b ~= fix(b) && c == fix(c))
if ismember(m,M1) && ismember(d,D1)
valid = true;
elseif ismember(m,M2) && ismember(d,D2)
valid = true;
elseif m==2 && ismember(d,F2)
valid = true;
else
valid = false;
end
elseif a == fix(a) || b == fix(b)
if ismember(m,M1) && ismember(d,D1)
valid = true;
elseif ismember(m,M2) && ismember(d,D2)
valid = true;
elseif m==2 && ismember(d,F1)
valid = true;
else
valid = false;
end
end
end
  2 Comments
Walter Roberson
Walter Roberson on 16 May 2020
What if nargin is not 3 ?
Is it possible for both your if a and elseif a to fail? If so what should happen? If not then you do not need all of the tests that you have.
OUSSAMA El GABBARI
OUSSAMA El GABBARI on 22 Jan 2022
I guess he forgot to put return after every valid = false and valid = true line.. right ??

Sign in to comment.


Cyrus David Pastelero
Cyrus David Pastelero on 29 Jun 2020
Edited: Cyrus David Pastelero on 29 Jun 2020
%this is my stupid answer
function valid = valid_date(year, month, day)
if isscalar(year) && isscalar(month) && isscalar(day) && month <= 12 && day <= 31 && year > 40 && day > 0
if(mod(year,400) == 0 || (mod(year,4) == 0 && mod(year,100) ~= 0))
if(mod(month,2) ~= 0 && day <= 31 && month <= 7)
valid = true;
elseif(mod(month,2) == 0 && month ~= 2 && day <=30 && month <=7)
valid = true;
elseif(mod(month,2) == 0 && month ~= 2 && day <=31 && month >=8)
valid = true;
elseif(mod(month,2) ~= 0 && month ~= 2 && day <= 30 && month >=8)
valid = true;
elseif(month == 2 && day <= 29)
valid = true;
else
valid = false;
end
else
if(mod(month,2) ~= 0 && day <= 31 && month <= 7)
valid = true;
elseif(mod(month,2) == 0 && month ~= 2 && day <=30 && month <=7)
valid = true;
elseif(mod(month,2) == 0 && month ~= 2 && day <=31 && month >=8)
valid = true;
elseif(mod(month,2) ~= 0 && month ~= 2 && day <= 30 && month >=8)
valid = true;
elseif(month == 2 && day <= 28)
valid = true;
else
valid = false;
end
end
else
valid = false;
end
end
  2 Comments
Walter Roberson
Walter Roberson on 29 Jun 2020
This is not MATLAB code. MATLAB does not permit // comments.
Cyrus David Pastelero
Cyrus David Pastelero on 29 Jun 2020
Yes, my bad. I'm a newbie in MatLab. I'm used in c++ syntax.

Sign in to comment.


ARNAB KARAK
ARNAB KARAK on 8 Jul 2021
% but this is not wirking on non scalar variable
function valid = valid_date(year,month,day)
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
max_days=31;
elseif (month==4||month==6||month==9||month==11)
max_days=30;
elseif mod(year, 4) == 0 && mod(year, 100) ~= 0|| mod(year, 400) == 0
max_days=29
else max_days=28
end
if (month<1) || (month>12)
valid=false
elseif day<1 || day>max_days
valid=false
else
valid=true
end
  2 Comments
Shikhar Srivastava
Shikhar Srivastava on 12 Nov 2021
Edited: Jan on 12 Nov 2021
function valid = valid_date(year,month,day)
if (mod(year,4)==0 || mod(year,400) ==0 && mod(year,100)~=0)
if (month==2)
valid = isscalar(date) && 0<date<30 && isscalar(year) &&year>0;
elseif (1<=month<=12 && month~=2)
valid = isscalar(date) && 0<date<=31 && isscalar(year) &&year>0;
end
end
if (mod(year,4)~=0 || mod(year,400) ~=0 && mod(year,100)==0)
if (month==2)
valid = isscalar(date) && 0<date<=28 && isscalar(year) &&year>0;
elseif (1<=month<=12 && month~=2)
valid = isscalar(date) && 0<date<=31 && isscalar(year) &&year>0;
end
end
end
please tell what is wrong
Jan
Jan on 12 Nov 2021
Edited: Jan on 12 Nov 2021
Please do not inject a question as a comment of an answer of another question. Open a new question instead.
The it is usful to mention, why you assume your code is wrong. You do have this important information already, so sharing it with the readers ist a good strategy.
One problem of your code: 0<date<30 will no do what you expect. This is evaluated from left to right. 0<date is either true or false, which are interpreted as 1 or 0. Both values are < 30 in every case. You want:
0 < date & date < 30
This appears multiple times in your code.
Another problem is that the variable valid is not defined under some conditions.
A hint to simplify the code: After:
if (month==2)
it is not need to test for month ~= 2 in the other branch:
elseif 1 <= month & month <= 12 % not needed: && month~=2

Sign in to comment.


Nasir Rehman
Nasir Rehman on 29 Jan 2022
function valid= valid_date(year,month,day)
if isscalar(year) && isscalar(month) && isscalar(day)
if mod(year,400) == 0 || (mod(year,4) == 0 && mod(year,100) ~= 0)
if month==2 && (day>=1 && day<=29)
valid=true;
elseif (month == 1 || month == 3||month==5||month==7|| month == 8||month==10||month==12) && (day>=1 && day<=31)
valid=true;
elseif (month == 4 || month == 6 || month==9 || month==11) && (day>=1 && day<=30)
valid=true;
else
valid=false;
end
elseif month== 2 && (day>=1 && day<=28)
valid=true;
elseif (month == 1 || month == 3||month==5||month==7|| month == 8||month==10||month==12)&& (day>=1 && day <=31)
valid = true;
elseif (month == 4 || month == 6 || month==9 || month==11) && (day>=1 && day <=30)
valid=true;
else
valid =false;
end
else
valid =false;
end
  5 Comments
Rik
Rik on 30 Jan 2022
Yes and no. This platform is indeed meant for questions and answers. The thing to consider is the value of an answer.
There is a difference between a homework question and a non-homework question. The first is a teaching tool, so any answers posted here should be teaching tools as well. That is why your answer doesn't make sense. Did you post it as a teacher or as a student? If you posted it as a student you are posting it in the wrong place (this isn't a homework submission system), and if you posted it as a teacher your post confuses me (as it doesn't seem to teach anything the other answers already teach).
You might be interested in Cody. There you can solve questions and challenges. That platform doesn't care about any comments or duplicates. (note that to achieve the best scores there you will be encouraged to follow terrible coding practices)
Rik
Rik on 31 Jan 2022
What was so bad about your comment? I don't see why you would prefer to delete it.
You can (of course) disagree with my opinion. As you have replied, I will not delete this answer myself. I hope to convince you to delete it yourself and put your efforts into answering other questions. There are new questions every day, and everyone willing to help is welcome.
I am not affiliated with Mathworks in any way other than being a customer and a frequent contributor on Answers and the File Exchange. My main concern is keeping this forum clean and maximize the number of people receiving help.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!