Alex Rivera | Logout

What is the easiest algorithm to find the day of week of day zero of a given year?

Asked 2009-01-26T02:21:31.303
12

I am trying to figure out what the day of the week of day zero (January 1st) of a given year.

So far I have looked at the Wikipedia page 'Calculating the day of the week' but I was wondering if there is an easiest algorithm if you're just trying to find day zero.

Edit
Report

2 Answers

2
public String DayOfWeek()
{
    int dayofweek;
    int c,y,m,d; 
    int cc,yy;
    String dayString;
    //Im using the guassian algorithm for finding day of the week 
    cc = year/100;
    yy = year - ((year/100)*100);

    c = (cc/4) - 2*cc-1;
    y = 5*yy/4;
    m = 26*(month+1)/10;
    d = day;

    dayofweek = (c+y+m+d)%7;

    switch(dayofweek)
    {
        case 0: dayString = "Sunday";
        break;
        case 1: dayString = "Monday";
        break;
        case 2: dayString = "Tuesday";
        break;
        case 3: dayString = "Wednesday";
        break;
        case 4: dayString = "Thursday";
        break;
        case 5: dayString = "Friday";
        break;
        case 6: dayString = "Saturday";
        break;
        default: dayString = "Sorry Could not compute month :(";   
    }

    return dayString;
}

The code above is written in Java

not sure why it works but i found that algorithm deep in the bowels of a Google search and quickly jumped on it for my project. what you see above is a method i had to write for a project i was doing in my java class in college, so it was written by me, but the algorithm is not my own.

this method is guaranteed to work 100% if the time, i've tried multiple days throughout history and looked them up to confirm the correct answer was the one that was found by this method.

Let the date be DD/MM/CCYY (european format), where DD is the day of the month, MM is the month, CC the century-digits and YY the year within the century. So Wilma's birthday was 23/06/1994. Starting with the century CC-digits, calculate CC/4 - 2*CC-1 and remember the result. With all divisions in this exercise, discard any remainder and just keep the whole part. So, in our example, this is 19/4=4 minus 2*19=38 minus 1, giving minus 35.

Now, using the year YY, calculate 5*YY/4. In this example that's 5*94 =

answered 2012-09-28T22:10:47.130
-1

You could always keep a reference date, and then add days of the year (mod 7) to keep a running tally, but like Zach said, using built-in functions is going to be way easier.

answered 2009-01-26T02:33:42.510

Your Answer