Day of Week Algorithm
programmingalgorithms.com
programmingalgorithms.com
I had pictured this as being essentially Gauss's algorithm, but if Wikipedia is correct, this particular implementation is due to Tomohiko Sakamoto in 1993.
https://en.wikipedia.org/wiki/Determination_of_the_day_of_th...
Anyone know of other, similar websites but for python? Or even if not for python, just another website like this.
Thanks @cokernel!
public int dayOfWeek(int year, int month, int day) {
day += month < 3 ? year-- : year - 2;
return ((((23 * month) / 9) + day + 4 + (year / 4)) - (year / 100) + (year / 400)) % 7;
}
[1] https://en.wikipedia.org/wiki/Zeller%27s_congruenceExamples:
2008-06-11 (Wednesday). Post's function returns 2 instead of 3.
But, in my opinion, it would be nice to know "why". Sure, i can digg into it, spend a day to understand or can use Google to find the answer; But it would be awesome if they will provide that information on the pages to the algorithms.
I.e the SHA 256 Page: I really like it. But if i'm new in this field, and don't even know about it a simple Wikipedia link would be enough to give me an "introduction" to the Algorithm. And, without it: It stays nice :)
I know it's a personal choice, but why do some people think the week starts on a Sunday? I'm curious as to what the explanation is?
https://en.wikipedia.org/wiki/Names_of_the_days_of_the_week#...
This is taken from the Jewish understanding in Genesis 1:3 to 2:3. According to this tradition, the world was created on a Sunday, and that's why it's the first day (יום ראשון 'first day').
https://en.wikipedia.org/wiki/Genesis_creation_narrative#Six...
Early Christianity mostly adopted Sunday as the "Lord's Day". (You can still see the history of the two Western weekend days in Latin and Romance languages; for example in Portuguese Saturday is sábado 'sabbath' < L. sabbatum, while Sunday is domingo 'Lord's day' < L. dominicus.)
I chose to change the way I think and switched my calendars to start on Sunday. This allowed me to change my state of mind and start the week with activities that I enjoy and spare time.
It is 100% in my head, but it worked for me.
Sunday is defined in law as the start of the week in some bits of English law, so there's that.
Italy, Spain, Ireland, England... actually all european countries (AFAIK) use Monday as the start of the week. All countries with a strong christian tradition.
The only countries I know that use Sunday as the start of the week are the US and Israel.
A quick search in wikipedia[0] also mentions that it's used as the first day of the week in Hebrew tradition and in some muslim countries.
[0] https://en.wikipedia.org/wiki/Sunday#Position_in_the_week
At the moment there's some inconsistancy:
Sunday to Saturday: https://www.gov.uk/maternity-pay-leave/pay
Monday to Sunday: https://www.gov.uk/guidance/statutory-sick-pay-manually-calc...
Googling for ["sunday to saturday" inurl:gov.uk] or ["monday to sunday" inurl:gov.uk] shows a bunch of examplesof each.
EDIT: You asked for links, so here's something from 2009 asking why it's so hard to find a calendar that starts on a monday: http://www.theguardian.com/notesandqueries/query/0,,-200944,...
> Why is it so hard to find a calendar that starts the week on a Monday, not a Sunday? It can't just be my family who gets outraged by this.
And here's something else from 2009 which (weirdly) explaining why it's so hard to find calendars that start on Sunday, not Monday.
http://www.telegraph.co.uk/comment/letters/6495398/Officiall...
tl;dr England used Sunday as the first day of the week until they started using ISO standards for time.
No, the commenter seems to think it's some kind of convention, as he clearly stated. The original question was, "but why do some people think the week starts on a Sunday?", and the answer given explains why "some people" (i. e., those in the U. S.) might think of Sunday as the first day of the week. Move along, no U.S.-centric conspiracies to be found here.
- "weekend" (that's "week end") includes both Saturday and Sunday
- "beginning of next week" means Monday/Tuesday
- "next week" in general seems to start on the Monday
So to me, the convention in the US seems to be more about how calendars are displayed than how we think about the week. Our two models disagree, so it's reasonable to ask why.
On the other hand Friday night through Saturday night is party time.
You want Sunday as the first day? Fine, do your index zero-based and Sunday is Zero.
You want Monday as the first day? Fine, index 1-based and use 7 for Sunday.
Kind of brilliant.
DateTime.Today.DayOfWeek;
public DayOfWeek DayOfWeek {
get {
Contract.Ensures(Contract.Result<DayOfWeek>() >= DayOfWeek.Sunday);
Contract.Ensures(Contract.Result<DayOfWeek>() <= DayOfWeek.Saturday);
return (DayOfWeek)((InternalTicks / TicksPerDay + 1) % 7);
}
}
public enum DayOfWeek {
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
}
http://referencesource.microsoft.com/#mscorlib/system/dateti...
http://referencesource.microsoft.com/#mscorlib/system/dayofw... private DayOfWeek getDayOfWeek(int year, int month, int day) {
return LocalDate.of(year, month, day).getDayOfWeek();
}
@Test
public void getDayOfWeekTest() {
assertEquals(DayOfWeek.FRIDAY, getDayOfWeek(2016, 3, 25));
}
It's a built-in function in Java 8. Under the hood, one can see what it's doing is converting the given date to an `EpochDay`, taking the `floorMod` of that by 7, and then returning the index of the DayOfWeek enum. Basically: public enum DayOfWeek { MONDAY, TUESDAY... }
int dow0 = (int)Math.floorMod(toEpochDay() + 3, 7);
return ENUMS[dayOfWeek - 1]; static int
calc_wday(int year, int month, int day)
{
int a, y, m;
int wday;
a = (14 - month) / 12;
y = year + 4800 - a;
m = month + 12 * a - 3;
wday = day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 + 2;
wday = wday % 7;
return wday;
}
Source: https://github.com/ruby/ruby/blob/5396d8a1ab52b4da4f51991094... sub GetDayofWeek {
my ($year, $month, $day) = @_;
my @monthTable = (0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4);
my @DayofWeek = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday);
$year -= ($month < 3) ? 1: 0;
return $DayofWeek[($year + $year / 4 - $year / 100 + $year / 400 + $monthTable[$month - 1] + $day) % 7];
}https://en.wikipedia.org/wiki/ISO_8601#Week_dates
Week numbers are useful for comparing equal length business quarters. Each quarter has 13 weeks except the last which can have 14 (but not much business last week of year).
Seems a bit over complicated though, I wonder if it could be simplified the way the day-of-week calculation is simplified.
weekDay = (dateObj) => ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][dateObj.getDay()]