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

2.606 views (last 30 days)
Ajith Thomas
Ajith Thomas on 29 Jun 2019
Commented: Shenaz Fathima on 4 Aug 2020 at 5:16
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 (23)

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

  10 Comments

Show 7 older comments
Saurov Baidya
Saurov Baidya on 23 Jul 2020 at 17:55
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 at 21:07
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

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.


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

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

  0 Comments

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.


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

  3 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.


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

  3 Comments

Rik
Rik on 11 Jun 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.
Sarbojit Mukherjee
Sarbojit Mukherjee on 12 Jun 2020
Respect you as a MVP...But still the term "cheating" is already incorporated in this kind of discussion when we start taking a homework topic( Not by Me)....beside that understanding & realizing the programing concept may be helpful for the beginer for some time.....
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.

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.


Sakib Javed
Sakib Javed on 19 Jun 2020
Edited: Rik on 19 Jun 2020
function valid = valid_date(year,month,day)
if (~isscalar(year) || year<1 || year~=fix(year)) || (~isscalar(month) || month<1 || month~=fix(month)) || (~isscalar(day) || day<1 || day~=fix(day))
valid = false;
return
end
if month > 12 && day > 31
valid = false;
return
end
if month == 1 || month == 3 || month == 5 || month == 7 || month==8 || (month==10) || (month==12)
if day<=31
valid= true;
else
valid=false;
return
end
elseif (month == 4 || month==6 || month == 9 || month==11)
if day<=30
valid= true;
else
valid=false;
return
end
elseif (month == 2) && ( (year/4)==fix(year/4) && (year/100) ~=fix(year/100) || (year/400)==fix(year/400)) && day<=29
valid = true;
elseif month ==2 && day <=28
valid=true;
else
valid = false;
return
end
end

  1 Comment

Rik
Rik on 19 Jun 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.

Sign in to comment.


Saurov Baidya
Saurov Baidya on 23 Jul 2020 at 17:39
%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.



per isakson
per isakson on 29 Jul 2020 at 5:36
Edited: per isakson on 29 Jul 2020 at 5:46
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

  0 Comments

Sign in to comment.


Kaarmukilan  S.P.
Kaarmukilan S.P. on 30 Jul 2020 at 16:35
Edited: Kaarmukilan S.P. on 31 Jul 2020 at 3:54
% 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 at 22:38
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 at 3:58
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 at 5:24
Edited: per isakson on 2 Aug 2020 at 9:15
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 at 11:58
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 at 18:36
@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.


Translated by