Borrar filtros
Borrar filtros

Problem in finding correct date.

1 visualización (últimos 30 días)
Hrishikesh Das
Hrishikesh Das el 25 de Abr. de 2020
Respondida: Kulko Margarita el 12 de Feb. de 2021
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 comentario
Om Yadav
Om Yadav el 25 de Abr. de 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.

Iniciar sesión para comentar.

Respuestas (2)

Ameer Hamza
Ameer Hamza el 25 de Abr. de 2020
Editada: Ameer Hamza el 25 de Abr. de 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

Kulko Margarita
Kulko Margarita el 12 de Feb. de 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

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by