What's the day today (or other dates)?

PHP - 101 97 103 125 characters

  • Sakamoto Algorithm
  • 0 = Sunday

Code

<?php fscanf(STDIN,"%4d%2d%2d",$y,$m,$d);@$a=a032503514624;$y-=$m<3;$z=$y+1;echo($y+$y/4%$z-$y/100%$z+$y/400%$z+$a[$m]+$d)%7;

Note

Unfortunately, due to PHP's dynamic, weak typing, the Sakamoto algorithm doesn't function properly without explicitly flooring each division operation.


Javascript, 126 123 characters

Using Sakamoto's algorithm with 0 = Sunday:

prompt().replace(/(....)(..)(..)/,function(_,y,m,d){y-=m<3;alert((+d+y-~(y/4)+~(y/100)-~(y/400)+ +".621462403513"[+m])%7)})

I suspect the divisions can be collapsed, but right now I'm not seeing it.

Edit: Improved the divisions (no need to ~~ when you can just ~).


Ruby, 95 92 characters

Plain straightforward ruby implementation with 0:Monday, ...

p ((y=(d=gets.to_i)/(k=100)/k-((m=d/k%k)<3?1:0))+y/4-y/k+y/400+"squsptrotqro"[-m].ord+d%k)%7

Tags:

Date

Code Golf