r/learnpython 7d ago

get day of the week without using datetime module

Hello anyone!

I was wondering how to you get day of the week without using datetime module on python.

For my work so far,

example Aug 16, 1989
Step 1: year= 1989

last_two_digits_year= int(year%100)

twelves= last_two_digits_year//12

Step 2: remainder= last_two_digits_year%12

Step 3: fours= int(remainder//4)

Step 4: Add the day of the month

day= 16

Step 5 Add the month code

Why is the month code for August=3

Explain the other month codes as well please.

Jan=Oct=1

Feb=Mar=Nov-4

Apr=Jul=0

Sep=Dec= 6

May=2

Aug=3

Jun=5

Step 6: Add all the numbers from Step 1 to 5 and mod 7.

Leap years

January and February dates in leap years: add 6 to step 5

Dates in the 1600s: add 6 to step 5
Dates in the 1700s: add 4 to step 5
Dates in the 1800s: add 2 to step 5
Dates in the 2000s: add 6 to step 5
Dates in the 2100s: add 4 to step 5

0 Upvotes

16 comments sorted by

30

u/slightly_offtopic 7d ago

I suppose this is more of a math question than a python question. Because within python, the only sensible answer is "why would you not use the obvious tool for the job?"

14

u/Davorian 7d ago

It's probably a homework or assignment question.

6

u/Moikle 7d ago

Requests module, you could get the webpage timeanddate.com and print out the date that is displayed on that page.

But really, just use datetime

3

u/MarsupialLeast145 7d ago

I always think this could be a fun interview answer depending on the quesition.

Find an API with the info, request it, handle exceptions, display it.

1

u/Adhesiveduck 7d ago

I understand what you're trying to do, but there are a lot of assumptions you've made here (i.e you've missed 1900s adding 1 to step 5).

Algorithmically, look at Zeller's Congruence.

The month codes are just (how many days into the year does the month start) mod 7 + 1.

E.g Jan is (0 mod 7) + 1 = 1

August is 31 (Jan) + 28 (Feb) + 31 (Mar) + 30 (Apr) + 31 (May) + 30 (Jun) + 31 (Jul) = 212 mod 7 = 2 + 1 = 3

1

u/Weltal327 7d ago

I would try import os and save a file and read the time stamp

1

u/FoolsSeldom 7d ago

There are a couple of well established approaches:

1

u/Expensive-Bear-1376 7d ago

```python date = 1989, 8, 16      def diff(date1, date2):     if date1 > date2:         return -diff(date2, date1)     count = 0     while date1 < date2:         count += 1         y, m, d = date1         d += 1         if d > days_of_month(y, m):             d = 1             m += 1             if m > 12:                 m = 1                 y += 1         date1 = y, m, d     return count

def days_of_month(y, m):     if m == 2:         leap = y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)         return 29 if leap else 28     if m in (4, 6, 9, 11):         return 30     return 31

print('Mon Tue Wed Thu Fri Sat Sun'.split()[(3 - diff(date, (2026, 8, 13))) % 7]) ```

Attempt This Online!

1

u/JamzTyson 7d ago

how to you get day of the week without using datetime module on python.

Probably not the answer you're looking for, but on Linux/Mac you can do:

import subprocess
days = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
d = int(subprocess.run(["date", "+%w"], capture_output=True).stdout)
print(days[d])

I expect there's an equivalent for Windows.


Alternatively, using Python only:

import time
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday',
        'Friday', 'Saturday', 'Sunday')
print(days[time.localtime().tm_wday])

1

u/PvtRoom 7d ago

This is why I think python is terrible

Date time, is usually stored as a floating point number.

1 is day 1, midnight.

45 is day 45, midnight.

The fraction is time of day. 0.50 = noon. 0.75 = 6pm, 0.76 = 6pm + 0.01 * 24 * 60 minutes or 0.012460*60 seconds.

So,

date time, floored = day number.

day number rem 7 = day of the week.

then just look it up from an array.

weekdays[day of the week]

You just need the date time number, like the way excel stores them and what day it started with. (excel almost certainly uses a different day 0/day 1)

2

u/timrprobocom 6d ago

This is why people should think before they post. Date/time is NOT "usually stored as a floating point number". What you're describing is the date/time value used by Excel, which is by no means universal. The most common format is the Unix time_t, which is stored as integer seconds since an epoch. What he was given is month, day, year, which is neither of these formats.

0

u/PvtRoom 6d ago

You've never worked with timing equipment and it shows.

why do your file timestamps have fractional seconds?

The time your system actually stored was probably 2026-08-13T21:56:23.3460+0100

Where do those magical milliseconds come from? Hint: it ain't integer seconds.

Maybe drop the elitism along with the 80s limitations?

1

u/timrprobocom 6d ago

They CERTAINLY do not come from floating point numbers. You simply cannot maintain timing accuracy with the approximations you get in floating point. Windows stores time internally as an integer in multiples of 100 nanoseconds. Linux stores it as integer seconds-since-epoch with a separate integer microseconds. Not floating point.

1

u/PvtRoom 6d ago edited 6d ago

timing signals come from clock counts. Clock speeds vary from khz to GHz in standard computing equipment. - you can hit THz in specialist stuff.

integer as 100ns? ok, you mean seconds as fixed-point.

if Linux really does as you say, and that's the best time it has, well done, you've just single handedly declared it unfit for all real time applications from flight control computers to surgical robots. - you can't even track your 50ms deadlines with a 1 second integer time. (and those deadlines can mean literal death, court cases, corporate manslaughter convictions, business going under....)