MATLAB Answers

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

1.599 views (last 30 days)
Ajith Thomas
Ajith Thomas on 29 Jun 2019
Edited: Rik on 17 Sep 2020 at 16:13
unction valid=valid_date(year,month,date)
if nargin==3
valid1=true;
else valid=false;
return
end
v1=[year]; v2=[month]; v3=[date];
if isscalar(v1)==true && isscalar(v2)==true && isscalar(v3)==true
valid2=true;
else valid=false;
return
end
if year>0 && 0<month && month<=12 && 0<date && date<=31
valid3=true;
else valid=false;
return
end
a=year/4; b=year/100; c=year/400;
if rem(year,4)==0 && rem(year,100)~=0
valid4=true;
else valid4=false;
end
if rem(year,100)==0 && rem(year,400)~=0
valid5=true;
else valid5=false;
end
if rem(year,400)==0
valid4=true;
else valid4=false;
end
if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=29) || (month==4||6||9||11 && date<=30) && valid4==true && valid5==false
valid6=true;
else valid6=false;
end
if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=28) || (month==4||6||9||11 && date<=30) && valid5==true && valid4==false
valid7=true;
else
valid7=false;
end
if valid1==true && valid2==true && valid3==true && valid6==true
valid=true;
elseif valid1==true &&valid2==true && valid3==true && valid7==true
valid=true;
else
valid=false;
return
end
why this code is not working for 2018/4/31 and 2003/2/29? and other random dates. but is works for non scalar and random leap years

  5 Comments

Show 2 older comments
Julian Veran
Julian Veran on 18 May 2020
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
Walter Roberson
Walter Roberson on 18 May 2020
Julian,
before posting, please use the MATLAB editor to "smart indent". Then when you post, first click on the '>' button in the CODE toolbar section of the Answers editor, and then paste the code into the highlight box that appears: that will cause the code to be posted in formatted form.
Sucheta Roy
Sucheta Roy on 7 Jun 2020
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

Sign in to comment.

Answers (25)

SANTOSH KAMBLE
SANTOSH KAMBLE on 3 May 2020
Edited: SANTOSH KAMBLE on 4 May 2020
function [valid]=valid_date(year,month,date)
y=year;m=month;d=date;
if ~isscalar(y)|| ~isscalar(m) || ~isscalar(d)
valid=false;
return
end
if y>=1 && m==1 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
elseif y>=1 && m==3 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==4 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==5 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==6 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==7 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==8 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==9 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==10 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==11 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==12 && d>=1 && d<=31
valid=true;
else
valid=false;
end
end

  11 Comments

Show 8 older comments
Saurov Baidya
Saurov Baidya on 23 Jul 2020
function [valid] = valid_date(y, m, d)
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d)
valid = false
return
end
if y>=1 && ismember(m,[1 3 5 7 8 10 12]) && d>=1 && d<=31
valid=true;
elseif y>=1 && ismember(m,[4 6 9 11]) && d>=1 && d<=30
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
else valid = false
return
end
end
Walter Roberson
Walter Roberson on 23 Jul 2020
Notice that if you were to use
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d) || y < 1
then you would not have to keep checking y>=1
Khaled Bin Easin
Khaled Bin Easin on 18 Aug 2020
function valid = valid_date(year,month,day)
if isscalar(year) && isscalar(month) && isscalar(day)
valid = year>0 && ((12>=month) && (month>0)) && ((31>=day) && (day>0)) && year==fix(year) && month==fix(month) && day==fix(day) && (((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && day<=31) || ((month==4 || month==6 || month==9 || month==11) && day<=30) || (((((rem(year,4)==0 && rem(year,100)~=0) || (rem(year,400)==0)) && day<=29) || (day<=28)) && month==2));
else
valid=false;
end
What about this code!!

Sign in to comment.


Nitin Kumar
Nitin Kumar on 27 Apr 2020
Here, this might help you:
function [valid]=valid_date(y, m, d)
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d)
valid=false;
return
end
if y>=1 && m>=1 && m<=12 && (ismember(m, [4 6 9 11]) && ismember(d, [1:30])) || (ismember(m, [1 3 5 7 8 10 12]) && ismember(d, 1:31))
valid=true;
elseif m==2 && (mod(y,4)==0 && mod(y,100)~=0 || mod(y,400)==0 && mod(y,100)==0) && ismember(d, 1:29)
valid=true;
elseif m==2 && ismember(d, 1:28)
valid=true;
else
valid=false;
return
end
end

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 9 May 2020
This code is not validating that the year is an integer, and if the month is 2 it is not validating that the year is positive.

Sign in to comment.


utkarsh singh
utkarsh singh on 19 May 2020
function valid=valid_date(y,m,d)
if((isscalar(d)&&isscalar(m)&&isscalar(y))&& (all([y, m, d] > 0))&& (~any(rem([y,m,d],1)))&&(m<=12&&d<=31))
daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
if(isequal(rem(y,4),0) && ~isequal(rem(y,100),0)|| isequal(rem(y, 400), 0))
daysInMonth(2)=29;
end
md=daysInMonth(m);
if d<=md
valid=true;
else
valid=false;
end
else
valid=false;
end
end

  3 Comments

Sign in to comment.


Pratham N
Pratham N on 25 May 2020
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

tushar jain
tushar jain on 6 May 2020
function valid= valid_date (year, month , day)
if ~isscalar(year)||~isscalar(month)||~isscalar(day)
valid= false;
return
end
if year>=1 && month>=1 && month<=12 && (ismember(month,[4 6 9 11]) && ismember(day,[1:30])) || year>=1 && month>=1 && month<=12 &&(ismember(month,[1 3 5 7 8 10 12])) && (ismember(day,[1:31]))
valid= true;
elseif month== 2 && (mod(year,4)==0 && mod(year,100)~=0 || month==2 && mod(year,400)==0 && mod(year,100)==0) && (ismember(day,1:29))
valid= true;
elseif month==2 && ismember(day,1:28)
valid =true;
else
valid= false;
return
end
end

  5 Comments

Show 2 older comments
Rik
Rik on 12 May 2020
Step through your code line by line. Because you made the strange decision to post your code as an image I can't run it, but I would assume you would see it more clearly if you put else on a line by itself and use the smart-allign.

Sign in to comment.


per isakson
per isakson on 29 Jul 2020
Edited: per isakson on 29 Jul 2020
Would this function pass?
function [ valid, dt ] = valid_date( y, m, d )
% The names of the input arguments, (year,month,day), are they
% mandatory? Are these names chosen to shadow functions in the
% finance toolbox?
try
% datetime is "smart". Doc says: "Each element of DateVector
% should be a positive or negative integer value [...]. If an
% element falls outside the conventional range, datetime adjusts
% both that date vector element and the previous element."
dt = datetime( y, m, d );
% If datetime didn't adjust any element the input is a
% valid date.
% valid = all( [ year(dt)==y, month(dt)==m, day(dt)==d ] );
vec = datevec( dt );
valid = all( [ vec(1)==y, vec(2)==m, vec(3)==d ] );
catch
% datetime throws an exception when not all input values are
% integers
dt = datetime.empty;
valid = false;
end
end

  1 Comment

Rik
Rik on 13 Aug 2020
I would hope so. As an instructor I would applaud lateral thinking like this. Just as I would accept code where someone did this:
for n=1%use a loop because it is a requirement
output=sum(data,2);
end

Sign in to comment.


ADARSH VISAJI
ADARSH VISAJI on 2 May 2020
function [valid]=valid_date(y, m, d)
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d)
valid=false;
return
end
if y>=1 && m>=1 && m<=12 && (ismember(m, [4 6 9 11]) && ismember(d, [1:30])) || (ismember(m, [1 3 5 7 8 10 12]) && ismember(d, 1:31))
valid=true;
elseif m==2 && (mod(y,4)==0 && mod(y,100)~=0 || mod(y,400)==0 && mod(y,100)==0) && ismember(d, 1:29)
valid=true;
elseif m==2 && ismember(d, 1:28)
valid=true;
else
valid=false;
return
end
end

  0 Comments

Sign in to comment.


Yaksha SJ
Yaksha SJ on 4 May 2020
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 Comments

Sign in to comment.


Deep Raj Bhujel
Deep Raj Bhujel on 8 May 2020
This is the solution from my side. That is final.

  1 Comment

Walter Roberson
Walter Roberson on 12 May 2020
If month is not scalar, or day is not scalar, then your calculation of c1 will have a non-scalar value together with the && operator, which is an error.
Note that the comparison operators like == and ismember() && return logical values. There is seldom good reason to compare those values further with ==1

Sign in to comment.


Malsha Fernando
Malsha Fernando on 13 May 2020
% code working 100%
%Malsha_Fernando
function valid =valid_date(year,month,day)
if isscalar(year)&&isscalar(month)&& isscalar(day)
if (floor(year)==year)&&(floor(month)==month)&&(floor(day)==day)
if (month>0) && (month<13)
if rem(year,4)==0
if rem(year,400)==0 %leap year
if( month==2)
if (day>0)&&(day<=29)
valid=true;
else
valid=false;
end
elseif (month==1)||(month==3)|| (month==5)|| (month==7)||(month==8)||(month==10)||(month==12)
if (day>0)&&(day<=31)
valid=true;
else
valid=false;
end
else
if (day>0)&&(day<=30)
valid=true;
else
valid=false;
end
end
elseif rem(year,100)==0
%fprintf('not a leap year');
if month==2
if (day>0)&&(day<29)
valid=true;
else
valid=false;
end
elseif (month==1)||(month==3)|| (month==5)|| (month==7)||(month==8)||(month==10)||(month==12)
if (day>0)&&(day<=31)
valid=true;
else
valid=false;
end
else
if (day>0)&&(day<=30)
valid=true;
else
valid=false;
end
end
else
%fprintf('leap year');
if month==2
if (day>0)&&(day<=29)
valid=true;
else
valid=false;
end
elseif ((month==1)||(month==3)|| (month==5)|| (month==7)||(month==8)||(month==10)||(month==12))
if (day>0)&&(day<=31)
valid=true;
else
valid=false;
end
else
if (day>0)&&(day<=30)
valid=true;
else
valid=false;
end
end
end
else
%fprintf('normal year');
if month == 2
if (day>0)&&(day<29)
valid=true;
else
valid=false;
end
elseif (month==1)||(month==3)|| (month==5)|| (month==7)||(month==8)||(month==10)||(month==12)
if (day>0)&&(day<=31)
valid=true;
else
valid=false;
end
else
if (day>0)&&(day<=30)
valid=true;
else
valid=false;
end
end
end
else
valid=false;
end
else
valid=false;
end
else
valid=false;
end

  0 Comments

Sign in to comment.


Iccu OUMOUACHA
Iccu OUMOUACHA on 25 May 2020
function valid=valid_date(year, month, day)
if year<=0 || month<=0 || day<=0 || mod( year , 1 )~=0 || mod( month , 1 )~=0 || mod( day , 1 )~=0
valid=false;
return
end
if month<=12 && (ismember(month, [4 6 9 11]) && ismember(day, [1:30]))
valid=true;
elseif month<=12 && (ismember(month, [1 3 5 7 8 10 12]) && ismember(day, 1:31))
valid=true;
elseif month==2 && (mod(year,4)==0 && mod(year,100)~=0 || mod(year,400)==0 && mod(year,100)==0) && ismember(day, 1:29)
valid=true;
elseif month==2 && ismember(day, 1:28)
valid=true;
else
valid=false;
return
end
end
it works in Matlab, when I test a non-scalar inputs, it return false. But here it doesn't work!!!!! ((Assessment 2))
What is the problem?

  3 Comments

Rik
Rik on 25 May 2020
Then you should change your code.
If you think this comment is unhelpful: show the code you used to ensure the output is false for non-scalar inputs. That is the only way someone will be able to help you.

Sign in to comment.


jaya shankar veeramalla
jaya shankar veeramalla on 29 May 2020
function isvalid = valid_date(y, m, d)
if ~(isscalar(y) && isscalar(m) && isscalar(d))
isvalid = false;
elseif ~all([y, m, d] > 0)
isvalid = false;
elseif any(rem([y, m, d], 1))
isvalid = false;
elseif (m > 12) || (d > 31)
isvalid = false;
else
daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
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 Comments

Sign in to comment.


Aron Magesh
Aron Magesh on 6 Jun 2020
Edited: Aron Magesh on 6 Jun 2020
function isvalid = valid_date(y,m,d)
%ISVALID function takes integers as inputs for year, month and day and
%checks if they form a valid year.
%The INPUTS shall be scalar, integers and positive.
%The maximum month number shall also match the actual month name.
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d)% Check if the values are scalar
isvalid = false;
fprintf('Enter a scalar value.')
% Check if they are integers
elseif y~=fix(y) || m~=fix(m) || d~=fix(d)
isvalid = false;
fprintf('Only scalar inputs are allowed.')
% Check if the values are positive
elseif y<1 || m<1 || d<1
isvalid = false;
fprintf('Inputs shall be positive integers.')
elseif d>31 || m>12
isvalid = false;
fprintf('The month or day exceeds 12 or 31 respectively.')
else
daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
% Check for leap years
% Years divisible by 4 except exactly divisible by 100
% However, years exactly divisible by 400 are leap years
if rem(y,4)==0 && rem(y,100)~=0 || rem(y,400)==0
daysInMonth(2) = 29;
end
maxDay = daysInMonth(m);
if d>maxDay
isvalid = false;
fprintf('Number of days for the month exceeds the maximum value.')
else
isvalid = true;
fprintf('The inputs are valid date numbers.')
end
end

  0 Comments

Sign in to comment.


MOHD FARHAN
MOHD FARHAN on 7 Jun 2020
Edited: Rik on 7 Jun 2020
function out= valid_date(y,m,d)
if mod(y,4)==0 if mod (y,100)==0 if mod(y,400)==0 leap =true; else leap=false; end else leap =true; end else leap= false; end
if y>0 &m>0&d>0 &y==fix(y)&m==fix(m)&d==fix(d)&m<=12 &isscalar(y)&isscalar(m)& isscalar(d)
if (m==1 | m==3|m==5|m==7 |m==8 |m==10 |m==12)&d <=31 out=true; elseif d<=30 if m==2 if leap==true & d<=29 out =true; elseif leap==false &d<=28 out=true; else out=false; end else out=true; end else out =false; end else out= false; end

  4 Comments

Show 1 older comment
Rik
Rik on 7 Jun 2020
It may be approved by your homework grader, but it is horrible. It is unreadable with this layout and there are no comments or documentation explaining what this function does. Look at other answers in this thread to see how you can improve this.

Sign in to comment.


Philip ISHOLA
Philip ISHOLA on 9 Jun 2020
function valid = valid_date(year,month,day)
if mod(year,4)==0
if mod (year,100)==0
if mod(year,400)==0
leap =true;
else leap=false;
end
else leap =true;
end
else leap= false;
end
if year>0 & month>0 & day>0 &...
year==fix(year) & month==fix(month) & day==fix(day) &...
month<=12 & isscalar(year) & isscalar(month) & isscalar(day)
if (month==1 | 3 | 5 | 7 | 8 | 10 | 12) & day <=31
valid = true;
elseif day<=30
if month==2
if leap==true & day<=29
valid = true;
elseif leap==false &d<=28
valid = true;
else valid = false;
end
else valid = true;
end
else valid = false;
end
else valid = false;
end
%not working well, please help

Sarbojit Mukherjee
Sarbojit Mukherjee on 11 Jun 2020
Edited: Sarbojit Mukherjee on 11 Jun 2020
This is the accepted code
function valid=valid_date(year, month, day)
if ~isscalar(year)||~isscalar(month)||~isscalar(day)
valid = false
return
end
if year<=0 || month<=0 || day<=0 || mod( year , 1 )~=0 || mod( month , 1 )~=0 || mod( day , 1 )~=0
valid=false;
return
end
if month<=12 && (ismember(month, [4 6 9 11]) && ismember(day, [1:30]))
valid=true;
elseif month<=12 && (ismember(month, [1 3 5 7 8 10 12]) && ismember(day, 1:31))
valid=true;
elseif month==2 && (mod(year,4)==0 && mod(year,100)~=0 || mod(year,400)==0 && mod(year,100)==0) && ismember(day, 1:29)
valid=true;
elseif month==2 && ismember(day, 1:28)
valid=true;
else
valid=false;
return
end
end
%/and it is working fine

  5 Comments

Show 2 older comments
Rik
Rik on 12 Jun 2020
There are no comments or documentation in your code. How exactly is this promoting understanding of the concept? Especially since there are already complete solutions in this thread.
PIRC
PIRC on 17 Sep 2020 at 15:46
Hello Mr Mukherjee I clear mention . I never copy others work and upload here. All are my original work. I have no need to shame on this. This is business only . Even today education also one of the biggest business in the world. Are you understood.
Rik
Rik on 17 Sep 2020 at 16:13
There is no need for insults. And the assertion that you don't use this to cheat doesn't mean that nobody will use this to cheat.

Sign in to comment.


MICHAEL
MICHAEL on 16 Jun 2020
%here is something i get
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 Comments

Sign in to comment.


Saurov Baidya
Saurov Baidya on 23 Jul 2020
%AS per the question and the variables assigned the following way can also be referred to the shortest one. if you are new and stucked in the middle of the program then kindly use help ______ on the command screen of matlab.
function [valid] = valid_date(year, month, day)
if ~isscalar(year) || ~isscalar(month) || ~isscalar(day)
valid = false
return
end
if year>=1 && month>=1 && month<=12 && (ismember(month,[ 4 6 9 11]) && ismember(day,1:30) || ismember(month,[1 3 5 7 8 10 12]) && ismember(day,1:31))
valid = true
elseif month == 2 && (mod(year,4)==0 && mod(year,100)~=0 || mod(year,400)==0 && mod(year,100)==0) && ismember(day,1:29)
valid = true
elseif month == 2 && ismember(day,1:28)
valid = true
else
valid = false
return
end
end

  0 Comments

Sign in to comment.



Kaarmukilan  S.P.
Kaarmukilan S.P. on 30 Jul 2020
Edited: Kaarmukilan S.P. on 31 Jul 2020
% a = year;
% b = month;
% c = day;
% out = result if date is valid or not
function out = valid_date(a,b,c)
if ~(isscalar(a) && isscalar(b) && isscalar(c)) % to find if the arguments are scalar or not
out = false(1);
elseif a>0 && b>0 && c>0 % to check if the arguments are postive or not
if (b==1 || b==3 || b==5 || b==7 || b==8 || b==10 || b==12) && c<=31
out = true(1);
elseif (b==4 || b==6 || b==9 || b==11) && c<=30
out = true(1);
elseif b==2 && c==29 % Special case of February in leap year
if isequal(rem(a, 4), 0) && (~isequal(rem(a, 100), 0) || isequal(rem(a, 400), 0))
out = true(1);
else
out = false(1);
end
elseif b==2 && c<=28 % Special case of February in normal year
out = true(1);
else
out = false(1);
end
else
out = false(1);
end
end

  2 Comments

Rik
Rik on 30 Jul 2020
Why should this answer not be deleted? (there are already complete solutions in this thread, and because it is homework that just invites cheating)
I don't see what this answer is adding, especially since there aren't any comments in your code.
Kaarmukilan  S.P.
Kaarmukilan S.P. on 31 Jul 2020
Hi Rik
My answer won't add difference to this thread. But since I have done this piece of code as amateur, I shared. If this answer violates any norms, I am open to remove my answer.
Comments I have addded and rest of the code is self explanatory. If the problem statement is understood, then code is understandable.
Thanks

Sign in to comment.


Arghya Bandyopadhyay
Arghya Bandyopadhyay on 2 Aug 2020
Edited: per isakson on 2 Aug 2020
function valid = valid_date(year, month, day)
valid = false;
if isscalar(year) && isscalar(month) && isscalar(day) && year > 0 && month > 0 && day > 0
daysinMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
if isequal(rem(year, 4), 0) && (~isequal(rem(year, 100), 0) || isequal(rem(year, 400), 0))
daysinMonth(2) = 29;
end
if month <= 12
maxdate = daysinMonth(month);
if day <= maxdate
valid = true
end
end
end
end

  0 Comments

Sign in to comment.


Kevin Vadaliya
Kevin Vadaliya on 2 Aug 2020
function valid = valid_date(year,month,date)
y=year;m=month;d=date;
if ~isscalar(y)|| ~isscalar(m) || ~isscalar(d) || y<1 || (mod(y,1)~=0) || m<1 || (mod(m,1)~=0) || d<1 || (mod(d,1)~=0)
valid=false;
return
end
if y>=1 && m==1 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
elseif y>=1 && m==3 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==4 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==5 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==6 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==7 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==8 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==9 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==10 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==11 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==12 && d>=1 && d<=31
valid=true;
else
valid=false;
end
end

  5 Comments

Show 2 older comments
Rik
Rik on 3 Aug 2020
@Shenaz, did you try stepping through your code to see what happens for a specific input? And where does your code find out that (2020,04,31) is invalid?

Sign in to comment.


Nitish Thakur
Nitish Thakur on 8 Aug 2020
function valid = valid_date(year, month, day)
%checking for inputs if it is valid or not(positive integer scalar)
if ~isscalar(year) || year < 1 || year ~= fix(year)
valid=false;
return
elseif ~isscalar(month) || month < 1 || month ~= fix(month) || month > 12
valid=false;
return
elseif ~isscalar(day) || day < 1 || day ~= fix(day) || day > 31
valid=false;
return
end
% months with maximum days of 31
if month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12
max_days=31;
if day > max_days
valid=false;
else
valid = true;
end
return
end
% months with max days of 30
if month==4 || month==6 || month==9 || month==11
max_days1 = 30;
if day > max_days1
valid=false;
else
valid = true;
end
return
end
% year with 29 days in february(leap year)
if ( month==2) && ( ( rem(year,4)==0 && rem(year,100)~=0 ) || rem(year,400)==0 )
max_days2 = 29;
if day > max_days2
valid=false;
else
valid = true;
end
return
% year with 28 days in february (non leap year)
else
max_days3 = 28;
if day > max_days3
valid=false;
else
valid = true;
end
return
end
end

  0 Comments

Sign in to comment.


Capulus_love
Capulus_love on 11 Aug 2020
Edited: per isakson on 11 Aug 2020
function x = valid_date(year,month,day)
if nargin == 3
if isscalar(year) && isscalar(month) && isscalar(day)
if month > 0 && month <= 12 && day >= 0 && year > 0
if (month == 1 || 3 || 5 || 7 || 8 || 10 || 12 && day <= 31)...
|| (month == 2 && day <= 29) && (month == 4 || 6 || 9 || 11 && day <= 30)
c0 = mod(year,4)
c1 = mod(year,100)
c2 = mod(year,400)
if (c0 == 0 && c1 ~=0) || (c1 == 0 && c2 ~= 0) || c2 == 0
x = 'true'
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
end
why this code not working? :(

  1 Comment

Rik
Rik on 11 Aug 2020
Let's try some cases that will likely point us to an issue:
valid_date(2020,2,29) %returns true
valid_date(2021,2,29) %returns false
valid_date(2021,2,28) %returns false
That last one is a problem. Can you follow the flow of your code where it should mark Feb 29 as valid only in leap years, and the rest of Feb as valid every year?
Also, your code returns the value as a char array, not a logical.

Sign in to comment.


Lokesh Sahu
Lokesh Sahu on 29 Aug 2020 at 14:53
function [valid] = valid_date(year, month, date)
format compact
%Check of all are positive integers
if ~isscalar(year) || ~isscalar(month) || ~isscalar(date)
valid = false;
return
end
% check month 31
if year>=1 && ismember(month,[1 3 5 7 8 10 12]) && date>=1 && date<=31
valid = true;
% check month 30
elseif year>=1 && ismember(month,[4 6 9 11]) && date>=1 && date<=30
valid = true;
% check february non leap
elseif year>=1 && month==2 && date>=1 && date<=28 && mod(year,4)~=0
valid = true;
% check february leap
elseif year>=1 && month==2 && date>=1 && date<=29 && ((mod(year,4)==0 && ...
mod(year,100)~=0 )|| mod(year,400)==0)
valid = true;
else valid = false;
return
end

Community Treasure Hunt

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

Start Hunting!

Translated by