# Python Finding the day from a given date

#### Taylor_1989

Summary
Disclaimer : The is a technical assessment sent to me by a potential employer, so I am not looking for any kinda of answer to the question more of guidance to a particular question I have with regards to the question. For full transparency to given employer I will link this thread so they can view the thread if they want.
The question is as follows :

''Given that January 1st, 2018 is a Saturday, write an algorithm to determine the day of the week for a given date provided as an input. The input to the algorithm should be in the format YYYYMMDD, where March 21st 2018 is written as 20180321''

My issue is as follows, looking through some algorithms on wiki https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week. My inital through were to implement one of the given on this page, but with further reading I realized this cannot be done for this question imo without modifications that I am not sure are possible. The reason being is that the given start date they have given is incorrect, in that 1st, 2018 is a Saturday which is not, and it is actually on a Monday which is the issue.

The given algorithms will given the true date, based on the Gregorian calendar and as the start date day is wrong I done belive I can use these type of algorithms and makes me think that I should not be looking at the general case but rather the specific case for that particular year, which I believe can be done.

Related Programming and Computer Science News on Phys.org

#### mfb

Mentor
Pretending that date is a Saturday instead of a Monday just shifts the week by two days.

Staff Emeritus
Could you write an algorithm that gives one of seven flavors of ice cream given the day of the week of the date given? If so, you've just solved this problem too.

#### jedishrfu

Mentor
There’s a very simple algorithm that I found on an old HP calculator.

Roughly:

Code:
month=month-3
If month<0:
month=month+12
year=year-1

days = truncate(year*365.25 + month*30.6 + day - basedays)

day_of_week=days%7
The idea is to adjust the month number so that March is month 0 and February is the last month so that every fourth year adds a day. You use 30.6 days per month and truncate the calculation. This gives you a date converted to days.

To calibrate the algorithm take a known date use it’s days as a base. Usually Sunday is the preferred day so you have to adjust your base number by adding a day or so.

Lastly, subtract yours date’s day number from th base date day number and use module 7.

When you do this for an interview, talk yourself through it and involve the interviewer. It doesn’t matter if you get it exactly right. It does matter if they like how you think through it.

A lot of programming involves implementing something wrong and through testing find the error and make it right.

Last edited:

#### Mark44

Mentor
Roughly:
Code:
Month=month-3
If month<0:
Month=month+12
Year=year-1

Days = year*365.25 + month*30.6 + day - basedays

Dayofweek=days%7
Given that the OP asked about an interview problem, this is probably good enough, but this algorithm suffers from not calculating leap days correctly (e.g., the rule is different in centuries that are divisible by 100 vs. those that are divisible by 400). It is also problematic in doing floating point arithmetic instead of integer arithmetic, which could cause Dayofweek to be off.

jedishrfu said:
A lot of programming involves implementing something tong and through testing find the error and make it right.
Autocorrupt? tough --> tong?

#### anorlunda

Mentor
No matter how elaborate you make the formulas, leap years, 400 year leap, ... it will have a finite upper bound of validity. I would write one complex enough to satisfy me, and then add a range check on the input date, and output an error if it is out of range.

Gold Member

#### jedishrfu

Mentor
Fixed it. The word was “wrong”

Yes, I didn’t mention the 100 or 400 year rules as the base date was in 2018. I imagine you’d get bonus points for mentioning that along with the fact that the Protestant groups didn’t adopt the Gregorian calendar until later and at different times. When I’d give this in my C class I’d ask them to compute for some date when the calendar actually skipped ten days ahead to realign it with the seasons.

The calendar mess would make a good time travel story where you wind up some days later than expected if you jumped some day before October 1582.

#### jedishrfu

Mentor
@Mark44 I had amended my post to include the truncate pseudo function to extract the integer portion that’s the key to the algorithm. It looks like you caught me in mid post again as I often post then find something wrong like caps or spell checking... and then fix them.

#### anorlunda

Mentor
Others have mentioned the 100 year and 400 year corrections. If you go longer, there are more corrections.
Because there are 365.2425 days in one year (rounded to the nearest 0.0001).

https://en.wikipedia.org/wiki/Category:Calendar_algorithms said:
Example: What is the day of the week of 27 January 8315?

8315-6300=2015, 2015-100=1915, 1915/100=19 remainder 15, 19x7=133, 133/9=14 remainder 7. 2015 is 700 years ahead of 1315, so 1315 is used. From table: for hundreds (13): 6. For remaining digits (15): 4. For month (January): 0. For date (27): 27. 6+4+0+27+50-14=73. 73/7=10 remainder 3. Day of week = Tuesday.
The real purpose of the question is to see whether your plan to handle invalid inputs. How about a negative year or non-numeric year as input?

Edit: They may also be testing to see if you are smart enough to ask for clarification of the requirements. Gregorian Calendar or Julian Calendar?

#### PeroK

Homework Helper
Gold Member
2018 Award
Summary: Disclaimer : The is a technical assessment sent to me by a potential employer, so I am not looking for any kinda of answer to the question more of guidance to a particular question I have with regards to the question. For full transparency to given employer I will link this thread so they can view the thread if they want.

The question is as follows :

''Given that January 1st, 2018 is a Saturday, write an algorithm to determine the day of the week for a given date provided as an input. The input to the algorithm should be in the format YYYYMMDD, where March 21st 2018 is written as 20180321''

My issue is as follows, looking through some algorithms on wiki https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week. My inital through were to implement one of the given on this page, but with further reading I realized this cannot be done for this question imo without modifications that I am not sure are possible. The reason being is that the given start date they have given is incorrect, in that 1st, 2018 is a Saturday which is not, and it is actually on a Monday which is the issue.
If they gave you the correct day of the week, you would just do what you've done: find an algorithm online and copy that. That would demonstrate very little understanding or capability on your part.

By giving you the wrong day of the week, they throw you onto your own initiative and at the very least test your ability to understand and modify an existing algorithm.

This trick actually used to be my party piece. There are a few calculations that you need to practise to be able to do it quickly. But, if you learned to do the calculations in your head and submitted yourself as the algortithm that might or might not impress them.

#### Taylor_1989

If they gave you the correct day of the week, you would just do what you've done: find an algorithm online and copy that. That would demonstrate very little understanding or capability on your part.

By giving you the wrong day of the week, they throw you onto your own initiative and at the very least test your ability to understand and modify an existing algorithm.

This trick actually used to be my party piece. There are a few calculations that you need to practise to be able to do it quickly. But, if you learned to do the calculations in your head and submitted yourself as the algortithm that might or might not impress them.
I understand where you coming from, and agree it was just that I was more confused if they were looking at a case where they wanted just 2018. I have taken all the above comments on board and currently working modifying one of the existing algorithms on the wiki link.

#### mfb

Mentor
Others have mentioned the 100 year and 400 year corrections. If you go longer, there are more corrections.
Not in the Gregorian calendar. And you can hardly be asked to make code for future calendars, predicting how they will change the leap year rule.

#### jedishrfu

Mentor
And we haven’t even mentioned the Earth’s slowdown which will change the 365.25 days constant.

Calendars math can be a real headache.

#### hmmm27

When programming, there is a certain amount of due diligence, professional standards require.

In this case, first thing you should do is call up the company and ask what day it is.

#### jedishrfu

Mentor
However, if this is a live interview asking what day today is will surely torpedo it.

#### hmmm27

Well, obviously the thing to do is show up two days before or after the scheduled meet, depending on how they've worded their request.

#### jedishrfu

Mentor
Sadly, that may fail too depending on whether corporate security picks you up for lingering, or for warwalking their site.

#### Svein

The following routine by Tom Van Baak will convert the times accordingly:
C:
/*
* Return Modified Julian Date given calendar year,
* month (1-12), and day (1-31). See sci.astro FAQ.
* - Valid for Gregorian dates from 17-Nov-1858.
*/

long
DateToMjd (int y, int m, int d)
{
return
367 * y
- 7 * (y + (m + 9) / 12) / 4
- 3 * ((y + (m - 9) / 7) / 100 + 1) / 4
+ 275 * m / 9
+ d
+ 1721028
- 2400000;
}

/*
* Calculate number of seconds since 1-Jan-1900.
* - Ignores UTC leap seconds.
*/

__int64
SecondsSince1900 (int y, int m, int d)
{
long Days;

Days = DateToMjd(y, m, d) - DateToMjd(1900, 1, 1);
return (__int64)Days * 86400;
}

#### jedishrfu

Mentor
Perfect for my time machine:

I couldn't afford a DeLorean.

#### .Scott

Homework Helper
With most computer languages, there is already a method for converting from day/month/year to some internal format which reflects elapsed time - usually in units of days. So using that conversion, or using methods similar to what was has been list in this thread, convert you date to that format. Then convert 1/1/2018 to that format and subtract it. The difference will be the number of days elapsed since 1/1/2018.
Next apply module and use it as an index into array {"Sat","Sun","Mon","Tue","Wed","Thu","Fri"}.
Python:
from datetime import date

Target = 20180321
TYear = Target//10000
TMonth = (Target//100)%100
TDay = Target%100
TDelta = date(TYear,TMonth,TDay)-date(2018,1,1)
TWeekDay = (TDelta.days)%7

Staff Emeritus
I couldn't afford a DeLorean.
I have a friend who has one. But he only drives it from time to time.

The real purpose of the question is to see whether your plan to handle invalid inputs.
I'm not sure that's the one single purpose. I could argue that the real purpose is to collect requirements. How far in the future does this need to work? (All the way to 9999?) And how far in the past? (All the way to 0001?) Does it need to handle the Julian-Gregorian change? If so, for what country?

#### mfb

Mentor
With most computer languages, there is already a method for converting from day/month/year to some internal format which reflects elapsed time
I don't think using them is the idea here. They also have methods to find the day of the week, if you use these libraries then all you do is subtracting 2.

#### Baluncore

And we haven’t even mentioned the Earth’s slowdown which will change the 365.25 days constant.
One advantage of counting integer days for a calendar is that the length of the day is irrelevant.

#### mfb

Mentor
It does influence the addition of leap years in the future.