Get every 5 weeks

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Fenderbridge
Posts: 3
Joined: 13 Aug 2022, 17:00

Get every 5 weeks

Post by Fenderbridge » 02 Mar 2023, 01:48

I am trying to make a bit of code, but cannot even imagine how it would work.

What I would really like is my macro to tell me if this week is week 1, 2, 3, 4, or 5.
If it is week 5, on Sunday at midnight it will turn to week 1.

The reason I need this is because I am making a scheduler within my program that will do certain things if it is week 2 or 5, but I am stuck and am not even sure where to start. I have managed roughly 4300 lines of code in this macro without getting help, but I am stumped!
This is what I have been staring at for the last 3 days trying to figure out how to make it do what I want to do, but my brain can't even comprehend the next step.

Code: Select all

var1 := A_YYYY . A_MM . A_DD
		var2 := 20230202 ; this date marks the start of rotating team 1
		EnvSub, var1, %var2%, days 
		MsgBox, %var1%
It gives me 27 (days since var2). Today should be week 5, next week should be week 1. Every time I think I get somewhere, I would have to be running the macro during a certain week to make it work properly without giving me an incorrect week number. I would like to be able to start this up any day or month, and be on the proper week number. I'm bamboozled, I hope you guys might have an idea!

Thank you!


[Mod action: Moved topic to v1 section. The main section is for v2.]
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Get every 5 weeks

Post by BoBo » 02 Mar 2023, 03:07

Why not go with calendar weeks (A_YWeek) instead? :think:
Fenderbridge
Posts: 3
Joined: 13 Aug 2022, 17:00

Re: Get every 5 weeks

Post by Fenderbridge » 03 Mar 2023, 00:08

BoBo wrote:
02 Mar 2023, 03:07
Why not go with calendar weeks (A_YWeek) instead? :think:
I'd really like to, I sat there and looked at "202309" intently trying to figure out how I would make that equivalent to "week 5", and how I would make 202310 = 1, 202311 = 2, 202312 = 3, 202313 = 4, 202314 = 5, 202315 = 1, etc etc etc

I know if I look at it long enough for enough days, eventually I'll figure something out, but right now I am 100% writer's blocked!
RussF
Posts: 1294
Joined: 05 Aug 2021, 06:36

Re: Get every 5 weeks

Post by RussF » 03 Mar 2023, 07:40

How about this:

Code: Select all

#Requires AutoHotkey v1.1.33+

; Define an arbitrary starting year
StartingYear    := 2020
; Define an offset value to align the current week with the one you want it to be
OffSet         := 0
; Calculate elapsed weeks since then
ElapsedWeeks    := ((A_YYYY - StartingYear) * 52) + SubStr(A_YWeek, -1) + Offset
; Use Modulo division to determine week #, adding 1 since there is no week 0
WeekNo          := Mod(ElapsedWeeks, 5) + 1

MsgBox, % "This is week #" . WeekNo
The one possible caveat that I can see has to do with the docs that say:
Precise definition of A_YWeek: If the week containing January 1st has four or more days in the new year, it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1.
I haven't had time to do a lot of experimentation, so I don't know if it is possible (using this logic) for a year to have 51 or 53 weeks. If so, your week # calculation may possibly be off by 1 at the beginning of a new year.

Russ
Fenderbridge
Posts: 3
Joined: 13 Aug 2022, 17:00

Re: Get every 5 weeks

Post by Fenderbridge » 03 Mar 2023, 15:11

RussF wrote:
03 Mar 2023, 07:40
How about this:

Code: Select all

#Requires AutoHotkey v1.1.33+

; Define an arbitrary starting year
StartingYear    := 2020
; Define an offset value to align the current week with the one you want it to be
OffSet         := 0
; Calculate elapsed weeks since then
ElapsedWeeks    := ((A_YYYY - StartingYear) * 52) + SubStr(A_YWeek, -1) + Offset
; Use Modulo division to determine week #, adding 1 since there is no week 0
WeekNo          := Mod(ElapsedWeeks, 5) + 1

MsgBox, % "This is week #" . WeekNo
The one possible caveat that I can see has to do with the docs that say:
Precise definition of A_YWeek: If the week containing January 1st has four or more days in the new year, it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1.
I haven't had time to do a lot of experimentation, so I don't know if it is possible (using this logic) for a year to have 51 or 53 weeks. If so, your week # calculation may possibly be off by 1 at the beginning of a new year.

Russ
You make it look so easy, thank you so much Russ, I super appreciate you taking the time and figuring out my problem! I'm going to spend some time to make sure this is going to give me the proper info that I need in case I have any further questions, but for now, DM me your venmo you superstar!
thanks!
RussF
Posts: 1294
Joined: 05 Aug 2021, 06:36

Re: Get every 5 weeks

Post by RussF » 06 Mar 2023, 15:34

No tips or compensation needed - just glad I could help.

Pay it forward when you can.

Russ
Post Reply

Return to “Ask for Help (v1)”