cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate week number in a year in flows?

I am new to here,I faced one issue in flow after digging into more finally i come to know there is no direct way to calculate week number in year.But in my business requirement its very important to get week number.can any one suggest how to calculate week number ?

 

Thanks 

Status: New
Comments
Level: Powered On

Here is how I handled this;

div(dayOfYear(utcNow()), 7)

utcNow() = Current date (or you can provide specific date by a variable

dayOfYear() = Wrap this expression around given date (for instance utcNow) and you get day of the year (ie. 1 February returns 32)

div() = Divide day of the year to 7, so you get week number. You don't need to round the number, because the remainder is not displayed as per my tests.

Level: Powered On

@UnprayingMantis sorry for ressurecting an old thread, but can you elaborate on how your expression looks?

 

I tried the following, but monday (which is the start day of the week in Denmark) renders as last week.

If(greater(div(float(dayOfYear(utcNow())),7),div(dayOfYear(utcNow()),7)),add(div(dayOfYear(utcNow()),7),1),div(dayOfYear(utcNow()),7))
Level: Powered On

Hi @Skeptik 

 

You don't need to use float function, because div function always returns integer, and not the remainder. However I understand your reasoning; I noticed that my expression depends on what week of the day you run on a specific year. Apparently my workflow was returning wrong week number since 2 months :$


Without spending too much efforts on perfectly formulazing, i believe below expression would achieve the result for this year.

add(div(dayOfYear(utcNow()), 7),1)

Level: Powered On

@UnprayingMantis thanks for replying back, i did some research in the meantime, my findings is as follows:

 

I've broken the code out into variables to get at better overview. As below, using greaterOrEqual returns the correct week number, as monday the 25th of february has a value of 8, and the 18th has a value of 7 and so on.

 

This returns wrong week number:

monday_greater.PNGWeek at monday using greater

This returns correct week number:

monday_greaterorequal.PNGWeek at monday using greaterOrEqual

Sunday and Tuesday is no problem because they return a floating value that is greater, not equal.

 

sunday_greater.PNGWeek at sunday using greater

sunday_greaterorequa.PNGWeek at sunday using greaterOrEqual

 

So you are saying that i can remove this:

If(greater(div(float(dayOfYear(utcNow())),7),div(dayOfYear(utcNow()),7)),add(div(dayOfYear(utcNow()),7),1),div(dayOfYear(utcNow()),7))

And replace it with this:

add(div(dayOfYear(utcNow()), 7),1)

Cheers!

Level: Powered On

Hi @Skeptik ,

 

Yes the given expression should work for year 2019, but won't work for 2020. I just come up with a quick work-around, but full-blown week number calculation (that works any year) will take much more efforts. You probably need to write an expression that follows complex formula (consider leap years, follow ISO 8601 -where week 1 of year should contain at least 4 days from the given year), if you want an ultimate solution (works on any year).