# Weekday

Determining the weekday based on a date composed of day, month and year.
The program counts up all the days from the 1st of January, 1 to the given date, divides it by $7$, looks at the remainder and returns the weekday.

``````# Python 2.7.7 Code
# Jonathan Frech 22nd of July, 2016``````

``````# Python 2.7.7 Code
# Jonathan Frech 22nd of July, 2016

# import needed modules
import datetime

# determine if a given year is a leap year
def leap(y):
# divisible by four
if y % 4 == 0:
# before 1600 leap years were easier to calculate
if y < 1600:
return True

# after 1600 every year divisible by 100 that is not divisible by 400 is not a leap year
if y % 100 == 0 and not y % 400 == 0:
return False

# the year is a leap year
return True

# the year is not a leap year
return False

# turn a date into a nice string
def format(d, m, y):
# get the day right
_ = int(str(d)[-1])
if d in [11, 12, 13]:
d = str(d) + "th"
elif _ == 1:
d = str(d) + "st"
elif _ == 2:
d = str(d) + "nd"
elif _ == 3:
d = str(d) + "rd"
else:
d = str(d) + "th"

# get the month right
m = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][m - 1]

# the year stays as it is
y = str(y)

# return
return d + " of " + m + ", " + y

# get a date's weekday
def weekday(d, m, y):
# determine temporal relation between given date and real date
now = datetime.datetime.now()
if (now.year > y) or (now.year == y and now.month > m) or (now.year == y and now.month == m and now.day > d):
time = "was"
elif now.year == y and now.month == m and now.day == d:
time = "is"
else:
time = "will be"

# these days do not exist
if y == 1582 and m == 10 and (d > 4 and d < 15):
print "The " + format(d, m, y) + " did not occur."
return False

# add up days (starting at 4)
days = 4 + d

# add up days in all month passed
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if leap(y):
months[1] += 1
for _ in range(0, m-1):
days += months[_]

# add up all days in all years passed
for _ in range(1, y):
days += 365
if leap(_):
days += 1

# subtract the 10 days that do not exist
if (y > 1582) or (y == 1582 and m > 10) or (y == 1582 and m == 10 and d > 14):
days -= 10

# print the date and the weekday
print "The " + format(d, m, y) + " " + time + " a " + ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"][days % 7] + ".\n"

print "\n" * 100
print "==============="
print " W E E K D A Y "
print "===============\n\n\n"

# examples
weekday(24, 12, 2016)
weekday(22, 7, 2016)
weekday(13, 12, 1999)
weekday(9, 11, 1989)
weekday(1, 1, 1970)
weekday(14, 7, 1789)
weekday(7, 10, 1582)

# print new lines
print "\n" * 100``````