Excel: calculate which occurance of a weekday, within the month, from any given date e.g. fifth Friday - excel

I am not very good at Excel. In the NHS (UK) we often schedule activity by looking at the columns on a calendar (e.g. All day theatre list Mondays week 1,3,5). This means on the first Monday, third Monday and if present fifth Monday. Using a printed calendar you can see the columns, for example Mondays in Jan 2023 where there are five.
When planning, it would be great to have a formula that would accept a date, and return the ordinal of the weekday for that month e.g.
Jan 02 2023 = 1
Jan 28 2023 = 4
Jan 29 2023 = 5
Jan 30 2023 = 5
I have searched and found the WEEKNUM function, but this counts rows on the calendar not giving the result I need.
Any help gratefully received
Kind Regards Gavin Holt

This should return those values:
=-INT(-(DAY(YourCellWithTheDate))/7)

Related

US convention different in spreadsheet functions (Libre, Google Sheets, etc.)

The Excel/Google-Sheets/LibreOffice function DAYS360() returns the number of days between two dates based on a 360-day year. 0 (default) is used for the US-based method and here are some examples
A = 30 Apr 2016, B = 29 Feb 2016, DAYS360(A, B) = -61
A = 29 Feb 2016, B = 30 Apr 2016, DAYS360(A, B) = 60
This seems ok according to the rules here
But the Excel/Google-Sheets/LibreOffice function YEARFRAC() returns the number of years, including fractional years, between two dates using a specified day count convention. Even here 0 (default) uses US method, (US (NASD) 30/360) which I presumed will also be equal to the value of number of days calculated by DAYS360 * the number of seconds in a day/number of seconds in 360 days. The values in the sheets are as follows
A = 30 Apr 2016, B = 29 Feb 2016, YEARFRAC(A, B) = 0.1666666667
A = 29 Feb 2016, B = 30 Apr 2016, YEARFRAC(A, B) = 0.1666666667
Since it can be seen that the absolute value of the DAYS360 is different by one, the YEARFRAC value is same and assumes 60 days according to the presumption made above, so are the US-based convention mentioned here is the same as mentioned for DAYS360.
If not, what are the exact rules for this one, or is there some other problem?
NOTE: Tested these values on Google Sheets and Libre Office.
DAYS360 parameter 3:
0 indicates the US method - Under the US method, if start_date is the last day of a month, the day of month of start_date is changed to
30 for the purposes of the calculation. Furthermore if end_date is the
last day of a month and the day of the month of start_date is earlier
than the 30th, end_date is changed to the first day of the month
following end_date, otherwise the day of month of end_date is changed
to 30.
1 or any other value indicates the European method - Under the European method, any start_date or end_date that falls on the 31st of
a month has its day of month changed to 30.
YEARFRAC parameter 3:
0 indicates US (NASD) 30/360 - This assumes 30 day months and 360 day
years as per the National Association of Securities Dealers standard,
and performs specific adjustments to entered dates which fall at the
end of months.
1 indicates Actual/Actual - This calculates based upon the actual
number of days between the specified dates, and the actual number of
days in the intervening years. Used for US Treasury Bonds and Bills,
but also the most relevant for non-financial use.
2 indicates Actual/360 - This calculates based on the actual number of
days between the specified dates, but assumes a 360 day year.
3 indicates Actual/365 - This calculates based on the actual number of
days between the specified dates, but assumes a 365 day year.
4 indicates European 30/360 - Similar to 0, this calculates based on a
30 day month and 360 day year, but adjusts end-of-month dates
according to European financial conventions.

Weeknum that doesn't start from 1st every year

How can i start weeknum from first week in 2018 and go on through 2019, 2020, 2021, 2022... without starting the count from 1 every year. Ex: if 2018 has 44 weeks i want the weeknum(1-1-2019) to return 45 not 1 and so on.
If you Week starts on Monday use:
=INT((A1-"1/1/2018")/7)+1
If your week starts on Sunday:
=INT((A1-"1/1/2018"+1)/7)+1

Find the end-of-month date from any date entered in MS Excel

I need to take the date from one cell, find the end of the month that that date falls into, then add one calendar month.
Example:
Enter jan 13: jan 13 end of month is Jan 31, + 1 month is Feb 28 (or 9 on leap year).
Feb 19: = March 31
March 2 = 30 April
March 30 = 30 April
March 31 = 30 April
etc etc
Is there an excel formula that will do this? I don't want any macro/ VBA stuff.
If you have 2007 or later than:
=EOMONTH("3/30/2016",1)
Use the Date function, add one to the month and set the Day to zero. This gives the last day of the month. To get the last day of the next month add two to the month.
=DATE(YEAR(E3),MONTH(E3)+1,0)
=DATE(YEAR(E3),MONTH(E3)+2,0)

Numerical month to character month in excel

Is there a pre-installed function that directly converts 08 to Aug, 10 to Oct?
Currently I use text(date(0,have,1),"mmm").
You could simplify like this: =TEXT("8/0","mmm").
Update
I've come up with a new technique: =TEXT(number * 30,"mmm"):
How it works
Dates are stored as numbers in Excel. The number 1 is the date Jan 1, 1900; 2 is Jan 2, 1900; etc.
The 30th day in 1900 is in January; the 60th day is in February; the 90th day is in March.
Every multiple of 30 between 30 and 360 is in a different month. So we can simply multiply 30 by a number between 1 and 12, and the TEXT function will give us the month.
You can use
MonthName(yourmonthNumber)
or
MonthName(yourmonthNumber, True) 'to abbreviate the name
Or did you mean a worksheet function?

how to calculate the number of the week in a year?

I would like to calculte the number of the week in a year. I see this post
In this post the acepted answer use this code:
public static int GetIso8601WeekOfYear(DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
However I see a problem. The number of the week is repeated if the last day of a month is in Sunday.
For example, the last day of March of 2013 year is in sunday. This week is the number 13th, is correct. But in April, how C# use always 6 weeks in a month to calculate the number of week, the first week of april has not any day of april, because all the days belong to march because the last day of the week is 30th March. So C# says that the first week of april is th 15th week, but this is incorrect, it has to be 14th.
So I would like to know if there are any way to calculate the number of a week in a right way.
EDIT:
I mean this:
In march, the last week is this:
25 26 27 28 29 30 31
This is the 13th, is correct.
In april, the first week is:
1 2 3 4 5 6 7
And this week is calculated as 15th.
So if I see the march calendar the last week is calculated as 13th and if I see the april calendar the last week of march is caluclated as 14th. This is incorrect.
SOLUTION:
DateTime dtCalendar = Calendar.DisplayDate;
int gridRow = (int)GetValue(Grid.RowProperty);
// Return the week of our adjusted day
int wueekNumber= System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(dtCalendar, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
if (dtCalendar.DayOfWeek == DayOfWeek.Monday)
{
gridRow = gridRow - 1;
}
Text = (weekNumbe r+ gridRow - 1).ToString();
Thanks.
The problem is that you are using the wrong CalendarWeekRule. To get the result you want you should use FirstDay. I have seen various codes in internet saying that you should use FirstFourDayWeek but, after some testing, I realised that the "right one" is FirstDay. I have tested it with your example and it delivers the right result: 14th week.
int targetYear = 2013;
DateTime targetDate = new DateTime(targetYear, 4, 1);
int week = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(targetDate, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);

Resources