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)
I want to make the date of startOf("week") its start from Monday on current week
Is it possible for make this? My code:
moment().startOf("week").toDate();
This startOf start from saturday proof startOf start from saturday
Take a look at the First Day of Week and First Week of Year section
From the docs (emphasis is mine)
Locale#week.dow should be an integer representing the first day of the week, 0 is Sunday, 1 is Monday, ..., 6 is Saturday.
So just by running this code before doing any calls to moment
// From 2.12.0 onward
moment.updateLocale('en', {
week : {
dow : 1
}
});
It will set the first day of the week to Monday.
After that calling moment().startOf("week").toDate() will give you a Monday.
I have the following dates
18/04/2019
19/04/2019
20/04/2019
21/04/2019
22/04/2019
I am creating a calendar to mark whether the date is a weekend or workday. I used the following formula =IF(WEEKDAY(D3,2)>5,"WEEKEND","WORKDAY " & COUNTA($B$3:B3))
This returns:
WORKDAY 1 18/04/2019
WORKDAY 2 19/04/2019
WEEKEND 20/04/2019
WEEKEND 21/04/2019
WORKDAY 5 22/04/2019
I would like it to return every time it is a workday to add the next number next to it
so
WORKDAY 1 18/04/2019
WORKDAY 2 19/04/2019
WEEKEND 20/04/2019
WEEKEND 21/04/2019
WORKDAY 3 22/04/2019
how would I alter my formula to do this
Assuming your calendar includes a single instance of all dates (or, at least, all work dates):
=IF(WEEKDAY(D3)>5,"WEEKEND","WORKDAY " & NETWORKDAYS($D$3,D3))
Any idea how to simplify this equation? Criteria for each region (APAC, LATAM, EMEA, North America) varies (hour/day cut offs).
Believe it or not, this used to be longer. Here is my best attempt:
Broken out by groupings:
=IF(A2="","",
IF(WEEKDAY(B2,2)=6,"Weekend Case",
IF(AND(C2<>"APAC",WEEKDAY(B2,2)=7),"WeekendCase",
IF(AND(C2="APAC",OR(AND(WEEKDAY(B2,2)=5,HOUR(B2)>=3),AND(WEEKDAY(B2,2)=7,
HOUR(B2)<18))),"Weekend Case",
IF(AND(C2="North America",OR(AND(WEEKDAY(B2,2)=5,HOUR(B2)>=18),
AND(WEEKDAY(B2,2)=1,HOUR(B2)<8))),"Weekend Case",
IF(AND(C2="EMEA",OR(AND(WEEKDAY(B2,2)=5,HOUR(B2)>=10),
AND(WEEKDAY(B2,2)=1,HOUR(B2)<2))),"Weekend Case",
IF(AND(C2="LATAM",OR(AND(WEEKDAY(B2,2)=5,HOUR(B2)>=15),
AND(WEEKDAY(B2,2)=1,HOUR(B2)<5))),"Weekend Case",
"Weekday")))))))
Logic:
IF A2 = Nothing: Nothing
All Regions:
If Sat: WEEKEND
North America, EMEA, LATAM
IF Sun: WEEKEND
APAC:
IF Friday After 3AM OR Sunday Before 6PM: WEEKEND
North America:
IF Friday After 6PM OR Monday Before 8AM: WEEKEND
EMEA:
IF Friday After 10AM or Monday Before 2AM: WEEKEND
LATAM:
IF Friday After 3PM OR Monday Before 5AM: WEEKEND
Else:
WEEKDAY
Sample for each region with each output below:
If you can use an external table for Lookups, you should be able to simplify it (and make it easier to change should the need arise)
Lookup table:
The numbers are the number of days to offset for the Monday (col I) and the Friday (col J). i.e. for APAC, + 6 hours is =6/24 days and +21 hours is =21/24 days. (Make sure to use formulas for this table and not the numbers that appear as those are rounded)
You can then use the following formula:
=IF(AND(WEEKDAY(B2+VLOOKUP(C2,H$2:J$5,2))>=1,WEEKDAY(B2+VLOOKUP(C2,H$2:J$5,3))<=5),"Weekday","Weekend Case")
I get the same results with it as your sample.
Create a reference sheet called Schedule, and load it as follows:
A B C
1 Region WE-start WE-end
2 APAC 503 718
3 EMEA 505 802
4 LATAM 515 805
5 North America 518 818
The format of the WE values are dhh where d=weekday and hh=hour. If weekday is a Sunday, I force it to an 8 to make the calculation that follows easier.
The following formula generates your Type and corrects the EMEA one.
=IF(AND(IF(WEEKDAY(B2)=1,700,0)+WEEKDAY(B2)*100+HOUR(B2)>=VLOOKUP(C2,Schedule!$A$2:$C$5,2), IF(WEEKDAY(B2)=1,700,0)+WEEKDAY(B2)*100+HOUR(B2)<VLOOKUP(C2,Schedule!$A$2:$C$5,3)),"Weekend Case","Weekday")
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);