Day of the week

Well, you can always use mktime() or localtime(), but also conside this elegant code by Tomohiki Sakamoto. I don’t think any shorter is possible.. ;)


/* 0=sunday */
int dayofweek(int y, int m, int d){
int t[] = {0,3,2,5,0,3,5,1,4,6,2,4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

Advertisement

2 Responses to Day of the week

  1. nepbabu says:

    Nice Jwala :)
    Also check out algorithm B @ http://www.users.zetnet.co.uk/egrichards/algorith.txt .. trivial to implement but a little bit longer than the version you presented. ;)

  2. jwalanta says:

    wow, that’s a handful.. thanx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.