MATLAB: Branching if statements when determining the days of the week

dateinputoutput

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?

Best Answer

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})