When is Thanksgiving?

  • Background

    Some holidays have fixed, easy-to-remember dates, like Oct 31, Dec 25, etc. Some, however, want to be troublesome. They're specified as things like "the first Monday in September" or "the fourth Thursday in November". How am I supposed to know when that is?

    All I know is that Thanksgiving is fast approaching, so I need a program to help me figure out when it is. Some people even say that it's tomorrow, so your program needs to be as short as possible to make sure I can re-type it in time.

    The Challenge

    Create a program or function that, given an up-to-four-digit year (e.g. 2015 or 1984), outputs or returns the date of the United States' Thanksgiving in that year. Thanksgiving is defined as the fourth Thursday of November according to the Wikipedia page. (Hint: that page also includes some interesting information on the date pattern.)

    Input: a decimal number with a maximum of four digits representing a year in the Common Era (C.E.). Examples: 987, 1984, 2101

    Output: the date, including month and day, on which Thanksgiving falls, or would fall if it existed, in that year. This may be in any reasonable format; use your best judgment. Use the Gregorian Calendar in all cases, even if it was not in use at the time.

    (Note: Make sure to handle leap years correctly!)

    Test cases
    Input 1:


    Output 1:

    Nov 26

    Input 2:


    Output 2:

    Nov 22


    Submissions will be scored in bytes. I recommend this website to keep track of your byte count, though you can use any counter you like.


    -25% to your score if you handle B.C.E. dates as negative numbers (e.g. -480 would be the year of the battle of Thermopylae).

    Negative test case input:


    Corresponding output:

    Nov 25

    This is , so the lowest score wins!

    Edit: I am marking Thomas Kwa's TI-BASIC submission as accepted. Don't let this discourage you from submitting new entries!


    Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

    To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

    # Language Name, N bytes

    where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

    # Ruby, <s>104</s> <s>101</s> 96 bytes

    If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

    # Perl, 43 + 2 (-p flag) = 45 bytes

    You can also make the language name a link which will then show up in the leaderboard snippet:

    # [><>](http://esolangs.org/wiki/Fish), 121 bytes

    var QUESTION_ID=64785,OVERRIDE_USER=45162;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"http://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?([\d.]+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;

    body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

    For anyone who is wondering when Thanksgiving will be coming up this year: Thanksgiving will be tomorrow .

    But I had thanksgiving on October 10th? I'm sorry to say your question is late.

    @JimmyJazzx brings up a good point. In Canada, Thanksgiving is on the second Monday in October. You should probably specify that you are talking about United States' Thanksgiving.

    @TheNumberOne Thanks! I've edited the challenge to clarify.

    Must we use that byte counter or may we use that byte counter or may we use our own? Some languages like APL, have their own character set, which counts bytes differently

    @Downgoat Use whichever byte counter you want. That's just the one I use personally and would recommend.

    Do you want the de facto Thanksgiving dates or the dates that follow your rule? Since 1789 (in the US), Thanksgiving Day was observed on inconsistent days. From 1863 to 1940 it was on the last Thursday of November. Since then it has been celebrated on the 4th Thursday of that month. https://www.archives.gov/legislative/features/thanksgiving/

    @DavidCarraher The legal holiday is on the fourth Thursday.

    @LegionMammal978, only since 1941 has Thanksgiving been taken to be on the 4th Thursday, even though it was officially celebrated before then.

    "Some holidays have fixed, easy-to-remember dates, like Oct 31, Dec 25" Those examples are easy to remember because they're really the same day: octal 31 == decimal 25. ;-)

    @Adrian McCarthy That wasn't unintentional.

    What should be the output for `0`?

    @Eth I've got Nov 22 with my program, probably posting it soon.

    @nicael I get that for `0` and `1`.

    @AdrianMcCarthy Whenever I forget when Christmas is, I always use this method of converting octal 31 to decimal :)

    For an answer that does the bonus, should there be a year 0 between -1 and 1 or not?

    What about Easter?

    Most answers using buildin date functions apply the Julian calendar before 1582 and implement the date jump from Oct 4 to Oct 15 in 1582. The challenge states that we should "use the Gregorian calendar *in all cases, even if it was not in use at the time.*", so for example 1400 *isn't* a leap year. Also, the gap of 10 days in 1582 (or 11 days in 1752, which is more accurate for America) makes 1582 and 1752 good test cases. I'd say the correct answer for 1752 is Nov 23. 1582 would be Nov 25 and 1400 is Nov 27.

    @Mego No, you need the month and day.

    Are we allowed to output in the format `MMDD` (e.g. `1126` for Nov. 26)?

    @ThomasKwa Sure, you can do MMDD.

  • lirtosiast

    lirtosiast Correct answer

    6 years ago

    TI-BASIC, 15 bytes * 0.75 = 11.25

    Tested on my TI-84+ calculator


    Thankgiving is November 29, minus the day of the week of September 1st, where 1 is Sunday and 7 is Saturday. This outputs in the format MMDD.

    Test cases: 2015 -> 1126, 1917 -> 1122, -480 -> 1125 have been verified. TI-BASIC seems to use the Gregorian calendar for all dates.

    TI-BASIC doesn't support negative years, but this gets the bonus because we add 10000 to the input. Because the Gregorian calendar has a period of 400 years, this doesn't change the day of the week.

    Holy crap. xD I never would've thought this would have worked like this, +1.

    Good idea to add 10000. Nice.

    I count 23 Letters. How can this be 15 Bytes ?

    @StephanSchinkel TI-BASIC uses tokens that are stored in the calculators memory, I believe all the characters here are 1 byte except for the tokens `dayofWK(` and `Ans` which are 2 and 1 bytes each.

    You tested it on a calculator? wow.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used