How do I calculate the next/previous ISO weekday number? - next

The ISO 8601 standard numbers weekdays 1 for Monday through to 7 for Sunday.
Given a weekday number from 1 to 7, how do you calculate the number of the next/previous weekday?
E.g. given 1, the next weekday number would be 2, and the previous would be 7.

The weekday numbers can be calculated with simple addition and modulo. The formulas are as follows:
Next weekday number
(weekday number % 7) + 1
Previous weekday number
((weekday number + 5) % 7) + 1
Using C# and NodaTime's IsoDayOfWeek, you can create two simple extensions methods like:
public static IsoDayOfWeek NextDay( this IsoDayOfWeek dayOfWeek ) => (IsoDayOfWeek)( (int)dayOfWeek % 7 + 1 );
public static IsoDayOfWeek PreviousDay( this IsoDayOfWeek dayOfWeek ) => (IsoDayOfWeek)( (int)( dayOfWeek + 5 ) % 7 + 1 );

Related

Get the first Friday of the Quarter of Today's Date

I was trying to get the first Friday of the quarter of today's date or any given date. Let's say the date is 12/06/2020, which falls to the 2nd quarter of the year. I should be able to get the first Friday of the 2nd Quarter and that should be April 3rd.
I was only been able to get the quarter of a given date, which you can see in the code below. Been stuck here for a while. Thanks in advance.
quarter = Int((Month(Now) + 2) / 3)
Here's a function that takes a date and returns the first Friday of the quarter:
Function FirstFridayOfTheQuarter(MyDate As Date) As Date
Dim FirstDayOfTheQuarter As Date
FirstDayOfTheQuarter = DateSerial(Year(MyDate), Int((Month(MyDate) - 1) / 3) * 3 + 1, 1)
FirstFridayOfTheQuarter = DateAdd("d", (13 - Weekday(FirstDayOfTheQuarter)) Mod 7, FirstDayOfTheQuarter)
End Function
This function is taking advantage of the Weekday function that returns:
1 for a Sunday
2 for a Monday
3 for a Tuesday
4 for a Wednesday
5 for a Thursday
6 for a Friday
7 for a Saturday
In case you want a non VBA solution:
My formula is:
=CEILING(EOMONTH(DATE(YEAR(A1);ROUNDUP(MONTH(A1)/3;0)+(ROUNDUP(MONTH(A1)/3;0)-1)*2;1);-1)-5;7)+6

Divide revenue across quarters in two years - Excel and SmartSheet

I need a formula to:
recognize the quarter for each date
divide the revenue in half for the start and end quarter.
Below is an example of the results I'm looking for. Need this to be a formula I can build in SmartSheet and/or Excel and transfer over to a SmartSheet.
Add a "helper row" with quarter number and year so you can use a SUMIFS/2
=(SUMIFS($[Est Revenue]#row, $[Start Date]#row, IFERROR((ROUNDUP(MONTH(#cell) / 3) + "" + YEAR(#cell)), 0) = [Q1 2020]$1) + SUMIFS($[Est Revenue]#row, $[End Date]#row, IFERROR((ROUNDUP(MONTH(#cell) / 3) + "" + YEAR(#cell)), 0) = [Q1 2020]$1)) / 2
The helper row would be quarter number before the 4 digit year. So 12020 would be quarter 1 2020.

Get day number within calendar week for a specific date

I have a data set which includes dates.
I need to split this out by week number for reporting purposes.
What I have so far is:
startDate variable containing 03/01/2015 (populated from data in spreadsheet)
startDay = Day(startDate)
startMonth = Month(startDate)
startYear = Year(startDate)
startWeek = Application.WorksheetFunction.WeekNum(DateSerial(startYear, startMonth, startDay))
which gives me week 1 in startWeek
However I know need to know how far into week 1 the date is.
So for this example, as the date is the 3rd of January, it includes 3 days of week 1
Meaning the reporting I'm putting together will only report on 3 days (as opposed to the full week)
The only way I've figured to do this so far is to calculate which day of the year the date is and the use a MOD calculation (basically divide by 7 and the remainder is how far into the week it is)
dayNumber = DateDiff("d", DateSerial(startYear, 1, 1), DateSerial(startYear, startMonth, startDay)) + 1
dayOfWeek = dayNumber Mod 7
This does work, but I was wondering if there was a nicer solution than this.
You could use a loop to determine how many days before startDate the week number changed:
Public Sub FindDaysInWeekNo()
Dim startDate As Date
startDate = DateSerial(2015, 1, 3)
Dim startWeek As Integer
startWeek = Application.WorksheetFunction.WeekNum(startDate)
Dim i As Integer
Do While startWeek = Application.WorksheetFunction.WeekNum(DateAdd("d", -i, startDate))
i = i + 1
Loop
Debug.Print i '= 3rd day in this week number
End Sub
The following table shows my comparison to the other suggested formulas and why I think that (refered to =WEEKNUM) my calculation is correct.
Note that if you assume 1st to 7th January will be week 1 (days 1 to 7) you cannot use the WeekNum function because this will give you a different result (see table above and note that the first week has only 6 days according to the WeekNum function). Also you cannot name this week number (as what everybody calls week number is defined as https://en.wikipedia.org/wiki/Week#Week_numbering).
Instead you will need to use …
Public Function AlternativeWeekNum(startDate As Date) As Integer
AlternativeWeekNum = WorksheetFunction.Days(startDate, DateSerial(Year(startDate), 1, 1)) \ 7 + 1 'note that this no normal division but a integer division and uses a backslash instead
End Function
to calculate the week number your alternative way, and …
Public Function AlternativeWeekNumDay(startDate As Date) As Integer
AlternativeWeekNumDay = WorksheetFunction.Days(startDate, DateSerial(Year(startDate), 1, 1)) Mod 7 + 1
End Function
to calculate the day in the alternative week.
You can use the Weekday() function for this:
=WEEKDAY(B4;2)
The second parameter mentions how you want your days to be counted (starting from Sunday or Monday, counting starting from 0 or from 1, ...).
dayOfWeek = (8 + Weekday(startDate) - Weekday(DateSerial(startYear, 1, 1))) mod 7
Just take the positive mod 7 of the difference between the current Day-Of-Week and the Day-Of-Week for the 1st January of whatever the year is

Finding the week number of the quarter in Excel VBA

I need a VBA function that will return the week number in a quarter from a given date.
For example,
Input of 1/1/2016 will return 1,
1/4/2016 will return 2,
10/1/2016 will return 1,
10/7/2016 will return 2,
11/11/2016 will return 7
I have a function in Excel that does this:
=IF(O48="","",WEEKNUM(O48)-WEEKNUM(LOOKUP(O48,DATE(YEAR(O48),{1,4,7,10},1)))+1)
But I am struggling to port it to VBA. Help? Thanks!
You can use DatePart to get the week number in the year, then subtract 13 weeks for each quarter (also available with the DatePart function):
Public Function WeekOfQuarter(inValue As Date)
WeekOfQuarter = DatePart("ww", inValue) - ((DatePart("q", inValue) - 1) * 13)
End Function

Python - count the number of sundays on the first day of month between two dates

I need to calculate the number of sundays by importing calendar, but I don't have a clue on how to determine if those sundays are on the first day of the month. Any guide please?
Use the standard datetime module. Here is a very simple naive algorithm to approach your problem:
1) Get the starting and ending dates to look between them
2) Iterate on all dates between the given two (using the date's ordinal)
3) Check whether or not it is Sunday first usind date.day and date.weekday
from datetime import date
date1 = date(2005, 1, 1)
date2 = date(2008, 1, 1)
date1_ord = date1.toordinal()
date2_ord = date2.toordinal()
cnt = 0
for d_ord in range(date1_ord, date2_ord):
d = date.fromordinal(d_ord)
if (d.weekday() == 6) and (d.day == 1):
cnt = cnt + 1
print("Number of Sunday 1'st days is {}".format(cnt))

Resources