udfGetLastDayOfMonth
str udfGetLastDayOfMonth (str, int)
str udfGetLastDayOfMonthV2 (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetLastDayOfMonth (strYmdHms, intOffset)
If intOffset >= 0 Then Return TimeSubtract (ItemReplace (1, 3, TimeAdd (strYmdHms, "0:" : 1 + intOffset : ":0"), ":"), "0:0:1")
Return TimeSubtract (ItemReplace (1, 3, TimeSubtract (strYmdHms, "0:" : -1 - intOffset : ":0"), ":"), "0:0:1")
EndIf
;..........................................................................................................................................
; The return value is a YmdHms datetime string, leaves Hms part as is.
; The second parameter intOffset can be set to select any month in time scale
; in relation to the datetime value of the first parameter strYmdHms.
; intOffset = 0 ... Current month, i. e. the month given by the first parameter.
; intOffset > 0 ... Positive offset pointing to month in the future.
; intOffset < 0 ... Negative offset pointing to month in the past.
;
; Detlev Dalitz.20010325.20090726.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetLastDayOfMonthV2 (strYmdHms)
Return ItemReplace (udfDaysInMonth (strYmdHms), 3, strYmdHms, ":")
;..........................................................................................................................................
; The return value is a datetime string in the same format
; short "Ymd" or long "YmdHms" as given by the input parameter strYmdHms.
;
; Detlev Dalitz.20010325.20090726.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDaysInMonth (strYmdHms)
intDaysInMonth = ItemExtract (ItemExtract (2, strYmdHms, ":"), "31,28,31,30,31,30,31,31,30,31,30,31", ",")
If intDaysInMonth == 28 Then intDaysInMonth = intDaysInMonth + udfIsLeapYear (strYmdHms)
Return intDaysInMonth
;..........................................................................................................................................
; The return value is an integer number in the range 28..31.
;
; Detlev Dalitz.20010325.20090726.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIsLeapYear (strYmdHms)
intYear = Int (ItemExtract (1, strYmdHms, ":"))
Return !(intYear mod 4) && (!!(intYear mod 100) || !(intYear mod 400))
;..........................................................................................................................................
; This user defined function "udfIsLeapYear" determines wether the given year is a leap year or not.
; Based on the Gregorian calendar, first established in 1582 by Pope Gregory XIII.
; The function returns a boolean value of @FALSE (0) or @TRUE (1).
;
; Detlev Dalitz.20010325.20090725.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

; udfGetLastDayOfMonth_1
strLastDom_2001_02 = udfGetLastDayOfMonth ("2001:02:01", 0)           ; "2001:02:28:00:00:00"

strLastDom_2001_02_Minus12 = udfGetLastDayOfMonth ("2001:02:01", -12) ; "2000:02:29:00:00:00"
strLastDom_2001_02_Plus12 = udfGetLastDayOfMonth ("2001:02:01", 12)   ; "2002:02:28:00:00:00"

strLastDom_This = udfGetLastDayOfMonth (TimeYmdHms (), 0)             ; e. g. "2009:07:31:13:40:05"
strLastDom_Next = udfGetLastDayOfMonth (TimeYmdHms (), 1)             ; e. g. "2009:08:31:13:40:05"
strLastDom_Prev = udfGetLastDayOfMonth (TimeYmdHms (), -1)            ; e. g. "2009:06:30:13:40:05"

strLastDom_This_Minus12 = udfGetLastDayOfMonth (TimeYmdHms (), -12)   ; e. g. "2008:07:31:13:40:05"
strLastDom_This_Plus12 = udfGetLastDayOfMonth (TimeYmdHms (), 12)     ; e. g. "2010:07:31:13:40:05"


; udfGetLastDayOfMonthV2
strLastDom_2000_02_2 = udfGetLastDayOfMonthV2 ("2000:02:01") ; "2000:02:29"
strLastDom_2001_02_2 = udfGetLastDayOfMonthV2 ("2001:02:01") ; "2001:02:28"
strLastDom_This_2 = udfGetLastDayOfMonthV2 (TimeYmdHms ())   ; e. g. "2009:07:31:13:40:05"

Exit