I'm hopeless with date calculations, but I've come up with the below. All I want to do is know what the date is for the first day of the previous month.
Surely there is a much better and more efficient way to calculate this than with the 2 x ugly AddDate() commands below? LOL!
Procedure.q FirstDayOfLastMonth()
d=Date(2022,1,15,0,0,0) ; Test with 15 Jan 2022 because we want 1 Dec 2021 returned.
;d=Date()
f.q=AddDate(d,#PB_Date_Month,-1) ; Go back one month.
f=AddDate(f,#PB_Date_Day,-(Day(f)-1)) ; And set the day to the 1st.
ProcedureReturn f
EndProcedure
d=FirstDayOfLastMonth()
Debug FormatDate("%dd/%mm/%yyyy",d) ; 1 Dec 2021
da=Date(2022,01,15,0,0,0)
d=AddDate(da,#PB_Date_Day,-Day(da)); get day number of month then delete it from month
Debug FormatDate("01/%mm/%yyyy",d) ; 1 Dec 2021 hard code 01
Norm
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
da=Date(2022,01,15,0,0,0)
d=AddDate(da,#PB_Date_Day,-Day(da))+1; get day number of month then delete it from month and add one day
Debug FormatDate("%dd/%mm/%yyyy",d) ;
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Sorry, I am having a difficult time with the day being before the month . It looks like your way is the best for now. here's the one I should've posted.
Procedure.q FirstDayOfLastMonth()
d=Date(2022,1,15,0,0,0) ; Test with 15 Jan 2022 because we want 1 Dec 2021 returned.
;d=Date()
If Month(d) = 1 ; January
f = Date(Year(d) - 1, 12, 1, 0, 0, 0)
Else
f = Date(Year(d), Month(d)-1, 1, 0, 0, 0)
EndIf
ProcedureReturn f
EndProcedure
d=FirstDayOfLastMonth()
Debug FormatDate("%dd/%mm/%yyyy",d) ; 1 Dec 2021
Hi BarryG
You didn't get the idea
You can add plus or minus how many seconds to any date to get what you want
45 I used is to suit your provided example that is it
It is so simple
Only one line of code you need
Check tomorrow and the day after and you will get the right answer
d = Date(2022,1,15,0,0,0) ; Test with 15 Jan 2022 because we want 1 Dec 2021 returned.
date = d-((Day(d)+29)*24*3600)
Debug FormatDate("%dd/%mm/%yyyy", date) ; Returns 2 Dec 2021, which is wrong.
I don't think there's a one-size-fits-all calculation that can be done in one step.
da=Date(2022,12,31,0,0,0) ; 31 Dec 2022, so we want 1 Nov 2022
da=AddDate(da,#PB_Date_Month,-1)-((Day(da)-1)*86400)
Debug FormatDate("%dd/%mm/%yyyy",da) ; Incorrectly shows 31 Oct 2022
Okay, I think I'll stick with my 2 x AddDate(), or maybe Kenmo's if speed tests show that it's faster.
Procedure.q FirstDayOfLastMonth(d.q)
ProcedureReturn AddDate(AddDate(d, #PB_Date_Month, -1), #PB_Date_Day, -(Day(d)-1))
EndProcedure
; Test With 15 Jan 2022 because we want 1 Dec 2021 returned.
d = FirstDayOfLastMonth(Date(2022,1,15,0,0,0))
Debug FormatDate("%dd/%mm/%yyyy", d)
; Test with today
d = FirstDayOfLastMonth(Date())
Debug FormatDate("%dd/%mm/%yyyy", d)
d = Date(2022,1,15,0,0,0) ; Test with 15 Jan 2022 because we want 1 Dec 2021 returned.
date=Date(Year(d)-Bool(Month(d)=1),Month(d)-1+(12*Bool(Month(d)=1)),1,0,0,0)
Debug FormatDate("%dd/%mm/%yyyy", date)