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.

318 views (last 30 days)
Rakeshwar Elango
Rakeshwar Elango on 27 Jun 2019
Answered: David Goh on 20 Jun 2022 at 6:44
Hi Folks,
I have tried and ended up in errors. For example(valid_date(2018, 4, 1) failed,The last day of every month
Variable valid has an incorrect value. valid_date(2000, 1, 31) failed..., Random leap years
Variable valid has an incorrect value. valid_date(1624, 2, 29) failed..., Random dates
Variable valid has an incorrect value. valid_date(1433, 6, 28) failed...)
Kindly point out the errors I have made!
function valid = valid_date(year,month,day)
if (nargin==3)
if (fix(valid_date)&&isscalar(valid_date))
if((rem(year,4)==0||rem(year,400)==0) && rem(year,100)~=0)
if((month==1||3||5||7||9||11) && 0<day<32)
valid=true;
elseif(month==2 && 0<day<30)
valid=true;
elseif((month==4||6||8||10||12) && 0<day<31)
valid=true;
else
valid=false;
end
else
if((month==1||3||5||7||9||11) && 0<day<32)
valid=true;
elseif((month==4||6||8||10||12) && 0<day<31)
valid=true;
elseif(month==2 && 0<day<29)
valid=true;
else
valid=false;
end
end
else
valid=false;
end
else
valid=false;
end
  3 Comments
Rakeshwar Elango
Rakeshwar Elango on 28 Jun 2019
Thank you Stephen and Guillaume as the newly invented statement of me didn't through any error so I was wandering wheather where it went wrong. Thank you for your suggesion!

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 27 Jun 2019
Looks like you are taking the same class as Rahul. Rather than repeat my answer here, I will simply direct you to the link:
  2 Comments
Karina Medina Barzola
Karina Medina Barzola on 8 Jun 2021
function valid = valid_date(year,month,day)
if (isscalar(year)&&isscalar(month)&&isscalar(day))&&(isinteger(year/4) || ~isinteger(year/4)) && ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && (day>0 && day<=31)
valid = true;
elseif (isscalar(year)&&isscalar(month)&&isscalar(day))&&(isinteger(year/4) || ~isinteger(year/4)) && ((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day>0 && day<=30)
valid = true;
elseif (isscalar(year)&&isscalar(month)&&isscalar(day))&&mod(year, 400) == 0 && month == 2 && (day>0 && day<=29)
valid = true;
elseif (isscalar(year)&&isscalar(month)&&isscalar(day))&&mod(year, 4) == 0 && mod(year, 100) ~= 0 && month == 2 && (day>0 && day<=29)
valid = true;
elseif (isscalar(year)&&isscalar(month)&&isscalar(day))&&~(mod(year, 400) == 0) && month == 2 && (day>0 && day<=28)
valid = true;
elseif (isscalar(year)&&isscalar(month)&&isscalar(day))&&~(mod(year, 4) == 0 && mod(year, 100) ~= 0) && month == 2 && (day>0 && day<=28)
valid = true;
else
valid = false;
end

Sign in to comment.

More Answers (9)

Martín Avalos Postigo
Martín Avalos Postigo on 28 Jun 2021
function valid = valid_date(year,month,day)
if sum(rem([year,month,day],1))==0 && sum([year,month,day]>0)==3
if ismember(month,[1,3,5,7,8,10,12]) && day<32
valid=true;
elseif ismember(month,[4,6,9,11]) && day<31
valid=true;
elseif month==2 && ismember(sum(rem(year,[4,100,400])==0),[1,3]) && day<30
valid=true;
elseif month==2 && ismember(sum(rem(year,[4,100,400])==0),[0,2]) && day<29
valid=true;
else
valid=false;
end
else
valid=false;
end

Jatan Shah
Jatan Shah on 5 Aug 2021
function valid = valid_date (year, month, day)
if nargin<3
valid = false;
elseif ~isscalar(year) || year<1 || year~=fix(year)
valid = false;
elseif ~isscalar(month) || month<1 || month~=fix(month) || month>12
valid = false;
elseif ~isscalar(day) || 1>day || day~=fix(day) || day>31
valid = false;
elseif (month==4 || month==6 || month==9 || month==11) && day>30
valid = false;
elseif month==2 && day>29
valid = false;
elseif month==2 && day>28 && (year/4) ~= fix(year/4)
valid = false;
elseif (year/100)==fix(year/100) && (year/400)~=fix(year/400)
valid = false;
else
valid = true;
end

S HARIPRAKASH
S HARIPRAKASH on 25 Aug 2021
valid date or not
function valid=valid_date(year,month,day)
%check whether the given input is scalar positive integer or not
%1<=day<=31
%1<=month<=12
if((~isscalar(year))||(~isscalar(month))||(~isscalar(day))||(year<=0)||(month<=0)||(day<=0)||(month>12)||(day>31)||(~(year==fix(year)))||(~(month==fix(month)))||(~(day==fix(day))))
valid=false;
return
end
%february has 29 days in leap year and 28 days in non leap year
if (month==2)
if((mod(year,4)==0 && mod(year,100)~=0)||(mod(year,400)==0))
if day<30
valid=true;
return
else
valid=false;
return
end
else
if day<29
valid=true;return
else
valid=false;return
end
end
%check whether the number of given days is within the limit of days that particular month has.
else
month_days=[31,28,31,30,31,30,31,31,30,31,30,31];
if day<=month_days(month)
valid=true;
return
else
valid=false;
return
end
end
end

VIGNESH B S
VIGNESH B S on 13 Oct 2021
function [valid] = valid_date(year,month,day)
Entered = 0;
if isscalar(year) == 0
valid = logical(0);
Entered = 1;
elseif isscalar(month) == 0
valid = logical(0);
Entered = 1;
elseif isscalar(day) == 0
valid = logical(0);
Entered = 1;
end
if year <= 0
valid = logical(0);
Entered = 1;
elseif month <= 0
valid = logical(0);
Entered = 1;
elseif day <= 0
valid = logical(0);
Entered = 1;
end
leap_year = 0;
if(mod(year,4) == 0)
leap_year = 1;
if mod(year,100) == 0
if mod(year,400) == 0
leap_year = 1;
else
leap_year = 0;
end
end
end
if [leap_year,month,day] == [0,2,29]
valid = logical(0);
Entered = 1;
end
if [leap_year,month,day] == [1,2,29]
valid = logical(1);
end
if month == 1
if day >= 32
valid = (0>1);
Entered = 1;
end
elseif month == 10
if day >= 32
valid = (0>1);
Entered = 1;
end
elseif month == 8
if day >= 32
valid = (0>1);
Entered = 1;
end
elseif month == 7
if day >= 32
valid = (0>1);
Entered = 1;
end
elseif month == 5
if day >= 32
valid = (0>1);
Entered = 1;
end
elseif month == 3
if day >= 32
valid = (0>1);
Entered = 1;
end
elseif month == 12
if day >= 32
valid = (0>1);
Entered = 1;
end
elseif month == 4
if day >= 31
valid = logical(0);
Entered = 1;
end
elseif month == 9
if day >= 31
valid = logical(0);
Entered = 1;
end
elseif month == 6
if day >= 31
valid = logical(0);
Entered = 1;
end
elseif month == 11
if day >= 31
valid = logical(0);
Entered = 1;
end
end
if month == 2
if day >= 30
Entered = 1;
valid = logical(0);
end
end
if month >= 13
Entered = 1;
valid = logical(0);
end
if Entered == 0
valid = logical(1);
end
end
%A HELL LINES OF CODES!!!!!!! BUT IT WORKED OUT !!

Karnajit Banik
Karnajit Banik on 10 Jan 2022
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

Fazal Hussain
Fazal Hussain on 19 Jan 2022
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

Erikc Anderson Cortez Benites
function valid = valid_date(year, month, day)
if isscalar(year) && isscalar(month) && isscalar(day)
if month < 1 || month > 12
valid = false;
else
if month == 2 && isleapyear(year)
valid = day <= 29 && day>0 ;
elseif sum(month == [1,3,5,7,8,10,12])
valid = day <= 31 && day>0;
else
valid = day <= 30 && day>0;
end
end
else
valid = false;
end
function re = isleapyear(year)
if mod(year,400) == 0
re = 1;
elseif (mod(year,4) == 0) && (mod(year,100)) ~= 0
re = 1;
else
re = 0;
end

Ravleen Kaur
Ravleen Kaur on 19 May 2022
function valid = valid_date(year,month,day)
if (isscalar(year) && (year>=1) && year==fix(year)) && (isscalar(month) && ((1<=month)&&(month<=12)) && (month==fix(month))) && (isscalar(day) && ((1<=day)) && (day==fix(day)))
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day<32
valid = true;
elseif ( month == 4 || month == 6 || month == 7 || month == 9 || month == 11) && day<31
valid = true;
else
valid = false;
end
if (month == 2)
if day<29
valid = true; return
elseif day<30 && rem(year,4)==0 && rem(year,100)~=0 || rem(year,400)==0
valid = true; return
else
valid = false; return
end
end
else
valid = false; return
end

David Goh
David Goh on 20 Jun 2022 at 6:44
function valid = valid_date(year,month,day)
if isscalar(year) == 1 && isscalar(month) == 1 && isscalar(day) == 1 && year > 0 && month > 0 && day > 0
if month <= 12
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
if day <= 31
valid = true;
else
valid = false;
end
elseif month == 2
if (rem(year,4)==0 || rem(year,400)==0) && (rem(year,400)~=100 && rem(year,400)~=200 && rem(year,400)~=300)
if day <= 29
valid = true;
else
valid = false;
end
else
if day <= 28
valid = true;
else
valid = false;
end
end
else
if day <= 30
valid = true;
else
valid = false;
end
end
else
valid = false;
end
else
valid = false;
end

Community Treasure Hunt

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

Start Hunting!

Translated by