How to get the week number of year

I would like to get the week numebr of year in relevance.
Basically, equivalent of “date +%U” in Linux

Thanks,
Dan

Hi Dan,

From what I can see there is not a built in inspector to provide the week number. @AlanM, @Aram, please correct me if I’m wrong.

Judging from the wikipedia article I read on calculating week dates, this is probably not something that we want to try and roll our own relevance solution with either. Because it looks like the calculation involves a lookup table factoring in the ordinal date and leap years. And you have to decide if a week starts on a Sunday or a Monday. Etc.

I assume you are on Windows since if you were on Linux you could just get the output of the date command as you posted. I did a search online and it looks like the most straightforward solution is using PowerShell as detailed here: For example, running powershell get-date -UFormat %V returns 22, which matches https://whatweekisit.com.

That being said, anything that runs a script or utility would have to be done in actionscript and not relevance.So that may be a pain depending on your use case.

You may roughly get an amount of weeks using the following relevance (it gets an amount of days till today and divides it by 7).
I used “1 + weeks” to count a partial calendar week, you may get rid of it if you only need an amount of full weeks:

q: (1 + preceding text of first " " of ((((it - ((january 01 of (year of it as integer))-day)) of current date) / 7) as string) as integer)
A: 22

Thanks Sean for the quick reply.
I’m trying to do some check using relevance or “action parameter” before kicking offf a bash shell. So basically, I want to have the week number outside of the shell script.

Cheers,
Dan

Thanks Vnovik,

It works perfectly.

CHeers,
Dan

I think this may be a more concise relevance statement to achieve hopefully the same thing:

((it / week) + 1) of (current date - (first (current day_of_week) of (january & current year)))

If today is Thursday, this gets the first Thursday in January and counts the number of weeks since that day.

1 Like

I reformatted @strawgate 's relevance a bit:

(it + 1) of (it / week) of (current date - it) of (first (current day_of_week) of it) of (january & current year)

I prefer to keep the flow of operations in a single direction and not combine them.

I think both @vnovik 's and @strawgate 's relevance could have issues in some edge cases. I feel like I need to wrap my head around them and test them more to be sure one way or the other.

I think @strawgate 's relevance has an issue if the current day of the week is not in the first week of the year or vis versa.


I actually find it hard to parse @vnovik 's relevance as written.

Part of the @vnovik 's relevance finds the first day of the year, which I would do like this:

Q: (january 01 of it) of (it as integer) of current year
A: Sun, 01 Jan 2017
T: 0.056 ms
I: singular date

Then to get the last day of last year:

Q: (it - day) of (january 01 of it) of (it as integer) of current year
A: Sat, 31 Dec 2016
T: 0.061 ms
I: singular date

Then to get the number of the day of the year:

Q: (it / day) of (current date - it) of (it - day) of (january 01 of it) of (it as integer) of current year
A: 152
T: 0.067 ms
I: singular integer

Then divide by 7 to get number of weeks, or just do this instead:

Q: (it / week) of (current date - it) of (it - day) of (january 01 of it) of (it as integer) of current year
A: 21
T: 0.054 ms
I: singular integer

Then add 1: (which is not exact)

Q: (it + 1) of (it / week) of (current date - it) of (it - day) of (january 01 of it) of (it as integer) of current year
A: 22
T: 0.054 ms
I: singular integer

I’m just basically spelling out step by step how I reverse engineered and reformatted this: (1 + preceding text of first " " of ((((it - ((january 01 of (year of it as integer))-day)) of current date) / 7) as string) as integer)


What are you using the resulting week number for? Is some room for error acceptable?

Both of our’s are estimates and are off by 1 about 1/3rd of the time. This relevance will produce an error for the first week of the year whereas mine will not.

1 Like

It appears the most precise answer is actually pretty simple:

(if (it = -1) then (52) else (it + 1)) of ((current date - (january 02 & current year)) / week)

It appears to be accurate for every day of 2017. Note, January 1st is technically week 52 of the previous year.

You can use the following relevance to compare a years worth of answers for the 3 different pieces of relevance:

(it,(if (it = -1) then (52) else (it + 1)) of ((it - (january 02 & current year)) / week), ((it / week) + 1) of (it - (first (current day_of_week) of (january & current year))) as string | "Error", (1 + preceding text of first " " of ((((it - ((january 01 of (year of it as integer))-day)) of it) / 7) as string) as integer) as string | "Error") of ((date "01 Jan 2017") + (it*day)) of (integers in (0,364))

Sample of Answer:

Q: (it,(if (it = -1) then (52) else (it + 1)) of ((it - (january 02 & current year)) / week), ((it / week) + 1) of (it - (first (current day_of_week) of (january & current year))) as string | "Error", (1 + preceding text of first " " of ((((it - ((january 01 of (year of it as integer))-day)) of it) / 7) as string) as integer) as string | "Error") of ((date "01 Jan 2017") + (it*day)) of (integers in (0,364))
A: ( Sun, 01 Jan 2017 ), 52, 0, Error
A: ( Mon, 02 Jan 2017 ), 1, 0, Error
A: ( Tue, 03 Jan 2017 ), 1, 0, Error
A: ( Wed, 04 Jan 2017 ), 1, 0, Error
A: ( Thu, 05 Jan 2017 ), 1, 1, Error
A: ( Fri, 06 Jan 2017 ), 1, 1, Error
A: ( Sat, 07 Jan 2017 ), 1, 1, 2
A: ( Sun, 08 Jan 2017 ), 1, 1, 2
A: ( Mon, 09 Jan 2017 ), 2, 1, 2
A: ( Tue, 10 Jan 2017 ), 2, 1, 2
A: ( Wed, 11 Jan 2017 ), 2, 1, 2
A: ( Thu, 12 Jan 2017 ), 2, 2, 2
A: ( Fri, 13 Jan 2017 ), 2, 2, 2
A: ( Sat, 14 Jan 2017 ), 2, 2, 3

Then you can compare those to this site: https://www.epochconverter.com/weeks/2017

2 Likes

And I’d rewrite it this way, because I’m very picky:

Q: ( if (it = -1) then (52) else (it + 1) ) of (it / week) of (current date - it) of (january 02 & it) of current year
A: 22
T: 0.057 ms
I: singular integer

This is definitely the right way to test this, which you beat me to pointing out.

Hi Jgstew/Strawgate/Vnovik/Sean,

Thanks for your input.
I think I should explain a bit more about what I want to achieve

I have this action

action parameter query "WeekNumber" with description "Enter the week number (1-52), or an empty entry to use the current week number: " with default value "30"

and I want to

  1. Use the current week as the default value. The week number calculation must be coupled with Linux “date +%U”.
  2. Check the entered number to make sure it isn’t greater than the current one

I’m currently doing that week check in a bash script of the action script. I want to see whether I can perform that at the console level.

Hope that clears things a bit.

Cheers,
Dan

1 Like

I understand what you want, but I don’t know what you are using the results of this relevance or the date command for on the actual system. Instead of using the date command, you could use a relevance substitution for the week number that way you will be assured that the results always match.

  • Are you trying to have something only run once a week? There may be better options.

It does seem like it is possible to do in relevance, but I’m not certain of the algorithm that the date command uses to know how to exactly match that in relevance.

From here: https://ss64.com/bash/date.html

 Week:
    W  Week of the year (00-52)

    V  Week of the year (01-53)
       If the week containing January 1 has four or
       more days in the new year, then it is considered week 1;
       otherwise, it is week 53 of the previous year, and the next week
       is week 1. Similar to ISO 8601 (but not 100% compliant.)

    U  Same as 'W'
  • How important is it that it exactly match the date command?

Hi Jgstew,
We have a service which will be changed or rolled over on Sunday. That service will have a week number reference tied to it so getting the right week number for the task it’s important.

Please note that the difference between W and U is that the week will start on a different day (U=Sunday, W=Monday).

Cheers,
Dan