Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return Quarter of last month
#1
Hi guys,

i'm new here, my first post Smile

So i need for several macros and autotext a global variable that contains the quarter of the last month.

- We have now November 2021 so the question is: To which quarter belongs October 2021 (previous month)?
- Send me this back in a variable (letztesQuartal) in this Style (October = 4th quarter in 2021): Q4/2021.
- This runs as function once when i start QM.

I get what i want with my code.  But i guess it could be written much better.
Any ideas?

Thanks for your ideas and stay healthy
Achim
Code:
Copy      Help
 
function'str

str Monat
str brauchbaresMonatsDatum
DateTime pcDatum.FromComputerTime

pcDatum.AddMonths(-1) ;; calc previous month
Monat = brauchbaresMonatsDatum.timeformat("{MM}" pcDatum) ;;format to month

int rechner = val(Monat) ;; val = converter like .ToInteger
rechner = (rechner + 2) / 3 ;; Formula if Ceiling isn't available

;; pull Year
str brauchbaresJahresDatum
str Jahr

Jahr = brauchbaresJahresDatum.timeformat("{yyyy}" pcDatum) ;;format Year

str Teileins
str Teilzwei
str Teildrei
str Teilvier

Teileins = "Q"
Teilzwei = rechner
Teildrei = "/"
Teilvier = Jahr

str+ letztesQuartal ;; str+ sets global available variable

letztesQuartal.from(Teileins Teilzwei Teildrei Teilvier)

out letztesQuartal
#2
There is really no need to use a global variable here or have the function run at startup.
You can call it on demand using a function and local variables easy.
Can also simplify the code a bit.

create a Function and call that function from code when ever you need the value.

Function QuarterOfLastMonth
Code:
Copy      Help
function str&lq 
DateTime pcDatum.FromComputerTime
pcDatum.AddMonths(-1) ;; calc previous month
lq.from("Q" (val(pcDatum.ToStrFormat("{MM}")) +2 /3) "/" pcDatum.ToStrFormat("{yyyy}"))

call this function any other function or macro 
example from another function or macro
Function Function4
Code:
Copy      Help
str letztesQuartal; QuarterOfLastMonth(letztesQuartal)
out letztesQuartal

for Autotext can call it like this.
Two possible ways shown below.
One using a subfunction to call the function and one without using a subfunction at all.

Autotext Autotext18
Trigger $t     Help - how to add the trigger to the macro
Code:
Copy      Help
/b/i/c
lq :sub.Sub1;;Get Quarter of last month
glq :QuarterOfLastMonth(_s);paste+ _s;;;;Get Quarter of last month
#sub Sub1
QuarterOfLastMonth(_s)
paste+ _s
#3
Big Grin 
Thank you Kevin.

I've chosen the global variable on startup because the last month doesn't change today. So for me there is no need to calc it again with every macro i use it in.
Now we have november 15th. The last month will not change in the next two weeks - no matter how often this variable will be recalculated until december 1st. 
So for me it is enough if this variable is calculated once a day when i fire up my PC and get to work.
Hmmmm... maybe it should be better declared as constant Huh
#4
Code:
Copy      Help
 
function str&lastMonthShort
DateTime pcDatum.FromComputerTime
pcDatum.AddMonths(-3) ;; calc september
lastMonthShort.from(val(pcDatum.ToStrFormat("{MM}")) pcDatum.ToStrFormat("{yy}"))

Hi guys,

could anybody please tell me how i can avoid that val takes the leading zero from MM (month in digits) in this Date function?
Output should be for example from january to september in 2021:
0121 to 0921. But at this point i get only 121 to 921.
(i modified AddMonths from -1 to -3 so we get september).

Many thanks

Achim
#5
val is not needed at all in this function.
simply use
Code:
Copy      Help
function str&lastMonthShort
DateTime pcDatum.FromComputerTime
pcDatum.AddMonths(-3) ;; calc september
lastMonthShort.from(pcDatum.ToStrFormat("{MM}") pcDatum.ToStrFormat("{yy}"))


Forum Jump:


Users browsing this thread: 1 Guest(s)