Weekday determination
Afficher commentaires plus anciens
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)
----------------------------------------------------------
Réponses (4)
the cyclist
le 9 Déc 2011
2 votes
Did you think of trying MATLAB's own the weekday() function?
1 commentaire
Jan
le 9 Déc 2011
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
le 9 Déc 2011
0 votes
datestr(datenum(2011,12,8),'ddd')
Andrei Bobrov
le 9 Déc 2011
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 commentaires
Jan
le 9 Déc 2011
Do you have a reference for this method?
Andrei Bobrov
le 9 Déc 2011
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
Jan
le 9 Déc 2011
Thanks, Andrei. I think, I know what "ОСТАТОК 7" means. It reminds me to the "Omega 13".
Andrei Bobrov
le 9 Déc 2011
this is "mod 7"
Jan
le 9 Déc 2011
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
...
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!