Validation of a date in given format

9 次查看(过去 30 天)
donia shane
donia shane 2019-3-6
评论: Rik 2022-1-31
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.

回答(5 个)

Vinay Ram Gazula
Vinay Ram Gazula 2020-4-23
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
编辑:Walter Roberson 2020-5-16
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 个评论
Walter Roberson
Walter Roberson 2020-5-16
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 2022-1-22
I guess he forgot to put return after every valid = false and valid = true line.. right ??

请先登录,再进行评论。


Cyrus David Pastelero
编辑:Cyrus David Pastelero 2020-6-29
%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 个评论
Walter Roberson
Walter Roberson 2020-6-29
This is not MATLAB code. MATLAB does not permit // comments.
Cyrus David Pastelero
Yes, my bad. I'm a newbie in MatLab. I'm used in c++ syntax.

请先登录,再进行评论。


ARNAB KARAK
ARNAB KARAK 2021-7-8
% 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 个评论
Shikhar Srivastava
Shikhar Srivastava 2021-11-12
编辑:Jan 2021-11-12
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 2021-11-12
编辑:Jan 2021-11-12
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

请先登录,再进行评论。


Nasir Rehman
Nasir Rehman 2022-1-29
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 个评论
Rik
Rik 2022-1-30
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 2022-1-31
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by