Which Day of the Week was my Birthday?

vegaseat 2 Tallied Votes 232 Views Share

A while ago I created a code snippet in C for the same question. Solving this question with Python is a lot simpler, and on top of that Python takes care of impossible dates with the appropriate error message.

# find the day of the week of a given date
# Python will trap impossible dates like (1900, 2, 29)
# tested with Python24     vegaseat    01aug2005

from datetime import date

# a typical birthday  year, month, day 
# or change it to your own birthday... 
birthday = date(1983, 12, 25)

dayList = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
# date.weekday() returns 0 for Monday and so on, so pick the day string from the dayList
print "The day of the week on %s was a %s" % (birthday.strftime("%d%b%Y"), dayList[date.weekday(birthday)])

Dani AI

Generated

A short expert note to sit above the replies from and : for ordinary modern dates, a standard date library is the safest and simplest choice. Two issues that come up for readers years later are worth calling out: how to handle historical dates (Julian vs Gregorian) and how to compute the weekday if you must do it in plain C without a date library.

Calendar caveats: the Gregorian reform began in 1582 but countries adopted it at different times, so the "local" weekday for a historical date depends on which calendar was in use where and when. If you want a single consistent arithmetic answer instead, use the proleptic Gregorian rules (the Gregorian rules extended backward). Python’s date type is defined that way and documents the supported year range. (docs.python.org)

If a C implementation is required, Zeller’s congruence is compact and reliable for the Gregorian (and has a variant for the Julian) — implement it with the standard month adjustment (count Jan/Feb as months 13/14 of the previous year) and map the result to whatever weekday numbering you prefer. Example (Gregorian case; returns 0 = Monday .. 6 = Sunday):

/* return 0 = Monday .. 6 = Sunday */
int weekday_monday0(int y, int m, int d) {
    if (m < 3) { m += 12; y -= 1; }
    int K = y % 100;
    int J = y / 100;
    int h = (d + (13*(m + 1))/5 + K + K/4 + J/4 + 5*J) % 7; /* h: 0=Saturday */
    return (h + 5) % 7; /* convert to 0=Monday..6=Sunday */
}

Zeller’s derivation, the Gregorian vs Julian variants, and implementation notes are collected in standard references. Test any implementation against a few known anchors (for example the 1582 switch dates and later national switches) so you get the right policy (local calendar vs proleptic) for your use case. (en.wikipedia.org)

Lingson 14 Light Poster

hi,

I don't know if this is supported in python 2.4 already or not, but I'm using python 3.1.

Alternative code:

from datetime import date

birthday = date(1983, 12, 25)

print('The day of the week on %s was a %s' % (birthday.strftime('%d%b%Y'), birthday.strftime('%A')))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.