Google Sheets / Microsoft Excel, 76 bytes
=let(d,workday(eomonth(A1,)+1,-1),text(d,"dddd d")&if(day(d)=31,"st","th"))
Put the month and year in cell A1
in one of the formats specified in the question (or any other valid date format), and the formula in B1
.
The formula gets the first day of the next month and then finds the previous weekday before that, which is the same as the last weekday of the specified month.
The day number of the last weekday of a month will never end in 2
or 3
, so it is enough to add st
when the day is 31
and use th
with any other date.
Tested theThe formula is for Google Sheets, but I tested it in the most recent version of Microsoft Excel for the web and it seems to work the fine there as well.
The Google Sheets epoch is 30 December 1899. The formula will work with dates from January 1900 to December 99999. In Microsoft Excel, the range of valid dates seems more limited, and formula will work from January 1900 to November 9999.
input | expected | formula |
---|---|---|
10/2023 | Tuesday 31st | Tuesday 31st |
February 2017 | Tuesday 28th | Tuesday 28th |
09/1674 | Friday 28th | #NUM! |
1/2043 | Friday 30th | Friday 30th |