Branching if statements when determining the days of the week

10 views (last 30 days)
Without using any of the date functions built into matlab, How do you take an input that is in the form xx/xx/xxxx, and displays in the command window as 4.9751e-004 (date entered was 01/01/2010) and display it back to the user as what was entered, and not the gibberish that matlab is displaying? Is there some sort of conversion that will display it properly that isn't built into matlab?

Accepted Answer

Oleg Komarov
Oleg Komarov on 9 Jul 2011
d = datenum('08/22/1989','mm/dd/yyyy')
datestr(d,'dd-mmm-yyyy')
EDIT: no builtin date fcns
% Input: mm/dd/yyyy
s = '03/12/2008';
% Decompose into month, day, year
[m,d,y] = dataread('string',s,'%d/%d/%d');
% Determine if leap year
if mod(y,400) == 0 || (mod(y, 100) ~= 0 && mod(y, 4) == 0);
monthdays = [31 29 31 30 31 30 31 31 30 31 30 31];
else
monthdays = [31 28 31 30 31 30 31 31 30 31 30 31];
end
% Day of year
yearday = sum(monthdays(1:m-1)) + d;
% For the day of week, starting from reference 02 Jan 0000 (Sunday)
rd = 2; % day for reference date
ty = 0:y-1; % vector of years except last one
ly = sum(mod(ty,400) == 0 | (mod(ty, 100) ~= 0 & mod(ty, 4) == 0)); % Leap years
td = numel(ty)*365 + ly + yearday - rd; % Days in between
weekdays = {'Sun','Mon','Tue','Wed','Thu','Fri','Sat'};
disp(weekdays{mod(td,7) + 1})
  2 Comments
Jared Singleton
Jared Singleton on 9 Jul 2011
I can't use any built in date functions within matlab unfortunately. I discovered that if I turned the input into a string I can manipulate it that way. Right now I'm trying to determine the best way to figure out the month, and through that numbered day it is within the year, and what day it actually falls on. (i.e, that 01/01/2010 is the 1st day of the year 2010 and falls on a Thursday) I don't think I can even use eomday which would help considerably.
Oleg Komarov
Oleg Komarov on 9 Jul 2011
Tested, it should work for dates > 02 Jan 0000.
Also 01/01/2010 is a friday.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!