Problem in finding correct date.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am writting a code to see whether a particular date is valid or not. The function takes three positive integer scalar inputs. If it is a valid date it returns true or else false.
When I try to use the following code in the command window
valid = valid_date(2018,4,1)
valid = valid_date(2018,4,31)
It returns false for both the cases. However it should return true for the first case and false for the second case.
My code is as follows
function valid = valid_date(year,month,day)
if ~isscalar(year) ||~isscalar(month) || ~isscalar(day);
valid = false;
else
valid = true;
end
if year<1 || month<1 || day<1;
valid = false;
else
valid = true;
end
if month==4||month==6||month==9||month==11 && day<=30
valid = true;
else
valid = false;
end
if month==1||month==3||month==5||month==7||month==8||month==10||month==12 && day<=31
valid = true;
else
valid = false;
end
1 commentaire
Om Yadav
le 25 Avr 2020
Your if else is creating problem. You assigning 0 or 1 in each if condition. Ultimately, only last assigment survives. Let me give you an example of this. You just put your month==4 condition in the end and you will get the answer correct for months 4,6,9,11 and incorrect for others.
Suggestion is, only give condition and assignment. And else in the last loop like
function valid = valid_date(year,month,day)
if ~isscalar(year) ||~isscalar(month) || ~isscalar(day)
valid = false;
end
if year<1 || month<1 || day<1
valid = false;
end
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) && day<=31
valid = true;
end
if (month==4||month==6||month==9||month==11) && day<=30
valid = true;
else
valid=false;
end
But again Feb is problem so I am highlighting where you are faulting.
Réponses (2)
Ameer Hamza
le 25 Avr 2020
Modifié(e) : Ameer Hamza
le 25 Avr 2020
Try this simpler version
function tf = valid_date(y, m, d)
[Y,M,D] = ymd(datetime(y, m, d));
tf = all([Y M D] == [y m d]);
end
Result
>> valid_date(2018,4,1)
ans =
logical
1
>> valid_date(2018,4,31)
ans =
logical
0
0 commentaires
Kulko Margarita
le 12 Fév 2021
function isvalid = valid_date(y, m, d)
% Check if the inputs are valid
% Check that they are scalars
if ~(isscalar(y) && isscalar(m) && isscalar(d))
isvalid = false;
% Check that inputs are positive
elseif ~all([y, m, d] > 0)
isvalid = false;
% Check that inputs are integers (not the data type)
elseif any(rem([y, m, d], 1))
isvalid = false;
% Check that m and d are below the max possible
elseif (m > 12) || (d > 31)
isvalid = false;
% The inputs could be a valid date, let's see if they actually are
else
% Vector of the number of days for each month
daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
% If leap year, change days in Feb
if isequal(rem(y, 4), 0) && (~isequal(rem(y, 100), 0) || isequal(rem(y, 400), 0))
daysInMonth(2) = 29;
end
maxDay = daysInMonth(m);
if d > maxDay
isvalid = false;
else
isvalid = true;
end
end
end
0 commentaires
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!