Weekday determination

I wnat determination the weekday whne I enter year ,month,and day. how should I do ? I try it
-----------------------------------------------------
year=input('enter year')
month=input('enter month')
day=input('enter day')
switch year
case (1700:1799)
y=4
case (1800:1899)
y=2
case (1900:1999)
y=0
case (2000:2099)
y=6
case (2100:2299)
y=2
case (2300:2399)
y=0
case (2400:2499)
y=6
case (2500:2599)
y=4
case (2600:2699)
y=2
end
x=mod(year,100)
x1=floor(x,4)
switch month
case 1
m=0
case 2
m=3
case 3
m=3
case 4
m=6
case 5
m=1
case 6
m=4
case 7
m=6
case 8
m=2
case 9
m=5
case 10
m=0
case 11
m=3
case 12
m=5
end
d=mod((y+x1+m+day),7)
switch d
case 0
s=Sunday
case 1
s=Monday
case 2
s=Tuesday
case 3
s=Wednesday
case 4
s=Thursday
case 5
s=Friday
case 6
s=Saturday
end
fprintf('%g',s)
----------------------------------------------------------

回答(4 个)

the cyclist
the cyclist 2011-12-9

2 个投票

Did you think of trying MATLAB's own the weekday() function?

1 个评论

Beside the fact that using toolbox function is efficient and stable, the algorithm in WEEKDAY is smart:
d = mod(fix(datenum(year, month, day)) - 2, 7) + 1

请先登录,再进行评论。

Fangjun Jiang
Fangjun Jiang 2011-12-9

0 个投票

datestr(datenum(2011,12,8),'ddd')
function [dd,ddd] = dayweekwiki(y,m,d)
dd = rem( ceil( d + 497 * (m / 4800 + y / 400) + 16870921 / 2400 ) - 1 , 7) + 1;
if nargout > 1
ds = {'Monday'
'Tuesday'
'Wednesday'
'Thursday'
'Friday'
'Saturday'
'Sunday'};
ddd = ds(dd);
end

4 个评论

Do you have a reference for this method?
Hi Jan! Only in russian language:
http://ru.wikibooks.org/wiki/%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%B2%D1%8B%D1%87%D0%B8%D1%81%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F_%D0%B4%D0%BD%D1%8F_%D0%BD%D0%B5%D0%B4%D0%B5%D0%BB%D0%B8
Thanks, Andrei. I think, I know what "ОСТАТОК 7" means. It reminds me to the "Omega 13".
this is "mod 7"

请先登录,再进行评论。

Jan
Jan 2011-12-9
Another approach:
function dd = mmm(y,m,d)
t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
y = y - (m < 3);
dd = mod(y + floor(y/4) - floor(y/100) + floor(y/400) + t(m) + d, 7) + 1;
About your original code:
switch year
case (1700:1799)
This is no valid Matlab syntax. Better use
if 1700 <= year && year <1800
...
elseif year < 1900
...

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

提问:

KD
2011-12-9

Community Treasure Hunt

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

Start Hunting!

Translated by