# Finding the day from a given date

• Python
• Taylor_1989
In summary: Determination_of_the_day_of_the_week, it is suggested to implement one of the algorithms given on this page. However, further reading shows that the given start date (January 1st, 2018) is incorrect, as it is a Saturday instead of a Monday. This makes it impossible to use the given algorithms without modifications.The given algorithms use the Gregorian calendar to determine the day of the week, which further complicates the issue of the incorrect start date. This leads me to believe that a specific case approach may be necessary, rather than a general algorithm.In summary, the question poses a problem with the given start date being incorrect, requiring modifications to the suggested algorithms on the wiki page.
Taylor_1989
TL;DR 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 believe 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.

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

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.

jim mcnamara
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:
jedishrfu said:
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?

Taylor_1989, jedishrfu and Arman777
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.

Taylor_1989
anorlunda said:
it will have a finite upper bound of validity.
How so ?

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.

https://www.britannica.com/story/ten-days-that-vanished-the-switch-to-the-gregorian-calendar
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.

Taylor_1989 and Klystron
@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.

Others have mentioned the 100 year and 400 year corrections. If you go longer, there are more corrections.
Arman777 said:
How so ?

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?

jedishrfu, Taylor_1989 and Arman777
Taylor_1989 said:
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.

jedishrfu and Taylor_1989
PeroK said:
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.

anorlunda said:
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 and anorlunda
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.

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
However, if this is a live interview asking what day today is will surely torpedo it.

FactChecker
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.

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
Perfect for my time machine:

I couldn't afford a DeLorean.

Klystron
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

jedishrfu
jedishrfu said:
I couldn't afford a DeLorean.

I have a friend who has one. But he only drives it from time to time.

anorlunda said:
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?

hmmm27 and jedishrfu
.Scott said:
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.

jedishrfu said:
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.

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

mfb said:
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.
It was a technical assessment sent from a potential employer - and its "given" is a falsehood.
Among all right answers, which is the one that is most impressive?

Last edited:
mfb said:
It does influence the addition of leap years in the future.
Are you suggesting we should worry about 100 thousand years in the future?

The leap years are very well defined long into the future, well beyond electronic computers.
It is more likely humans will invent a completely new parallel calendar than adjust an old one.

Our calendar does NOT need to remain rigidly locked to the seasons, solstices and equinoxes.
Climate change will make more difference than calendar date.

.Scott said:
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
Firstly thank you, for your comment. I am not sure if I am allowed to use the liabry functions. I have ended up using the zeffer algorithm as shown below with the modifcation to aligin with there day starting on the first of Jan.

Zeffer Algorithm:
# Building dictionary list to contain days of the week
days = {11: 'Sunday', 12: 'Monday', 13: 'Tuesday', 14: 'Wednesday', 15: 'Thursday', 16: 'Friday', 10: 'Saturday'}

# Input date from the user/ for now just string in put
a = '20180101'# Define variable for the year, month and day and converting integer values
year = (int(a[0:4]))
month = (int((a[5:6])))
day = (int(a[6:8]))

# if statements for jan and feb

if month == 1 or month == 2:
month += 12
year -= 1

# addtional calculations for the first two digits of the year at beinging and end

year_end = year % 100
year_begin = year // 100

d = day + 13*(month+1)//5 + year_end + year_end//4 + year_begin//4 - 2*year_begin
d = d % 7
d = d + 10print(days[d-2])

Taylor_1989 said:
I have ended up using the zeffer algorithm as shown below
# Define variable for the year, month and day and converting integer values
year = (int(a[0:4]))
month = (int((a[5:6])))
day = (int(a[6:8]))
I'm not able to find a description of the Zeffer date algorithm.

Also, the declarations above appear to be using incorrect indexes. Does the notation a[0:4] represent the characters in the string from index 0 through index 4? If so, that's five characters. And similarly in a[6:8], which I believe would be the three characters at indexes 6, 7, and 8.

I believe this is what you want:
year = (int(a[0:3]))
month = (int((a[4:5])))
day = (int(a[6:7]))
Edit: the above doesn't give the right results. It should be
year = (int(a[0:4]))
month = (int((a[4:6])))
day = (int(a[6:8]))

Last edited:
Mark44 said:
I'm not able to find a description of the Zeffer date algorithm.

Also, the declarations above appear to be using incorrect indexes. Does the notation a[0:4] represent the characters in the string from index 0 through index 4? If so, that's five characters. And similarly in a[6:8], which I believe would be the three characters at indexes 6, 7, and 8.

I believe this is what you want:
year = (int(a[0:3]))
month = (int((a[4:5])))
day = (int(a[6:7]))
Okay if my indexing is wrong then my algorithm is indeed incorrect, because with the indexing I was using I was getting the correct day, back the drawing board.

I got the algorithm from

https://en.wikipedia.org/wiki/Zeller's_congruence
Sorry I spelt it wrong I ment Zeller, my apologies

Mark44 said:
Does the notation a[0:4] represent the characters in the string from index 0 through index 4? If so, that's five characters. And similarly in a[6:8], which I believe would be the three characters at indexes 6, 7, and 8.

I believe this is what you want:
year = (int(a[0:3]))
month = (int((a[4:5])))
day = (int(a[6:7]))
The index slice is being used to read the year and month and day from the string as u mentioned. I am now slightly confused to how my slicing produces the desired result for the year as you are right the index [0:3] should read the first four characters of the string '2018' however when I run the index from [0:3] it printed:

Python:
# Input date from the user/ for now just string in put
a = '20180101'# Define variable for the year, month and day and converting integer values
year = (int(a[0:3]))
print(year)
print('')
y = (int(a[0:4]))
print(y)

Output:
201

2018

So now my question is why is it reading in 5 characters for 2018 and not 4?

Last edited by a moderator:
Taylor_1989 said:
The index slice is being used to read the year and month and day from the string as u mentioned. I am now slightly confused to how my slicing produces the desired result for the year as you are right the index [0:3] should read the first four characters of the string '2018' however when I run the index from [0:3] it printed:
That was my mistake in not remembering how Python does things with array slices. Your expression a[0:4] evaluates to the string from a[0] up to but not including a[4].
The portion of the string starting at a[4] up to but not including a[6] gives you the two characters at indexes 4 and 5. And similarly for the last two characters at indexes 6 and 7.

The following runs in Python for an array a with 8 characters:
Python:
year = (int(a[0:4]))
month = (int((a[4:6])))
day = (int(a[6:8]))

Taylor_1989

## 1. How do I find the day of the week from a given date?

To find the day of the week from a given date, you can use a calendar or a date calculator tool. You can also use a mathematical formula called the Zeller's congruence. This formula takes into account the year, month, and day of the date and gives you the day of the week as an output.

## 2. What is Zeller's congruence?

Zeller's congruence is a mathematical formula used to calculate the day of the week from a given date. It takes into account the year, month, and day of the date and gives you the day of the week as an output. This formula was developed by Christian Zeller in the late 19th century and is still widely used today.

## 3. Can I use Zeller's congruence for any date?

Yes, Zeller's congruence can be used for any date. The formula takes into account leap years and works for dates in the past, present, and future. However, it is important to note that this formula is not 100% accurate and may give incorrect results for dates before the 16th century.

## 4. Are there any other methods for finding the day from a given date?

Yes, there are other methods for finding the day from a given date. Some examples include using a calendar, a date calculator tool, or a computer program. You can also use the Doomsday algorithm, which is a simpler version of Zeller's congruence and is more accurate for dates before the 16th century.

## 5. Is it possible to find the day of the week without using any tools or formulas?

Yes, it is possible to find the day of the week without using any tools or formulas. This method is called the "fingers and knuckles" method and involves using your fingers and knuckles as a visual representation of the days of the week. With some practice, you can easily determine the day of the week for any given date using this method.

• Precalculus Mathematics Homework Help
Replies
3
Views
1K
• Programming and Computer Science
Replies
29
Views
3K
• General Math
Replies
1
Views
1K
• Computing and Technology
Replies
25
Views
3K
• Engineering and Comp Sci Homework Help
Replies
5
Views
2K
• Programming and Computer Science
Replies
2
Views
2K
• General Discussion
Replies
6
Views
2K
• Engineering and Comp Sci Homework Help
Replies
2
Views
2K
• Programming and Computer Science
Replies
8
Views
2K
• Astronomy and Astrophysics
Replies
15
Views
4K