Brent Fanguy

Last Buisness day of the month

9 posts in this topic

I have a scheduled task in windows that runs an autoit script during business hours daily.   I do not want to run this section of code if it is the last business day of the month (would be great if the logic included holidays but i am ok if it does not.

' something like If Now() <> LastBuisnessWorkDay Then

$Run = "C:\Infor\Vmfg654\vmaplutl.exe -d " & $Database & " -u SYSADM -p yourpsw"

Run($Run)

WinWaitActive("Costing Utilities - Infor ERP VE")

Send("!f")

Sleep(1000)

Send("n")

WinActivate ("Costing Utilities", "OK")

WinWaitActive("Costing Utilities","OK")

ControlClick("Costing Utilities", "OK", "Button1") WinClose("Costing Utilities")

'End If

Share this post


Link to post
Share on other sites



@Brent Fanguy welcome to the forum. Are you just looking for the last weekday of the month? If so there are a number of examples on the forum. Here is one that I use:

#include <Date.au3>
#include <MsgBoxConstants.au3>

MsgBox($MB_OK, "Last WeekDay", _lastWorkDay(4, 2016))

Func _lastWorkDay($iMonth, $iYear)
    Local $iLastWeekDay
    Local $iDaysInMonth = _DateDaysInMonth($iYear, $iMonth)
    Local $iWeekday = _DateToDayOfWeekISO($iYear, $iMonth, $iDaysInMonth)

    Switch $iWeekday
        Case 6, 7
            $iLastWeekDay = $iMonth & "/" & $iDaysInMonth - (($iWeekday = 7) ? 2 : 1) & "/" & $iYear
        Case Else
            $iLastWeekDay = $iMonth & "/" & $iDaysInMonth & "/" & $iYear
    EndSwitch

    Return $iLastWeekDay
EndFunc

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

This thread may be helpful in setting up an array to determine if a date is a holiday:

Share this post


Link to post
Share on other sites

thanks, guys.

what am i doing wrong?

#include <Date.au3>
#include <MsgBoxConstants.au3>

'MsgBox($MB_OK, "Last WeekDay", _lastWorkDay(2, 2016))

MsgBox($MB_OK, "Last WeekDay", _lastWorkDay(@MON, @YEAR))

$date = @MON & "/" & @MDAY & "/" & @YEAR
MsgBox($MB_OK, "current date", $date)

If $date = _lastWorkDay(@MON, @YEAR)) Then    ' i get an error on this line  - it does not like my If $date
   MsgBox($MB_OK,"=")
Else
   MsgBox($MB_OK,"<>")
EndIf


Func _lastWorkDay($iMonth, $iYear)
    Local $iLastWeekDay
    Local $iDaysInMonth = _DateDaysInMonth($iYear, $iMonth)
    Local $iWeekday = _DateToDayOfWeekISO($iYear, $iMonth, $iDaysInMonth)

    Switch $iWeekday
        Case 6, 7
            $iLastWeekDay = $iMonth & "/" & $iDaysInMonth - (($iWeekday = 7) ? 2 : 1) & "/" & $iYear
        Case Else
            $iLastWeekDay = $iMonth & "/" & $iDaysInMonth & "/" & $iYear
    EndSwitch

    Return $iLastWeekDay
EndFunc

Share this post


Link to post
Share on other sites

You put two ))  at end.

Saludos

Share this post


Link to post
Share on other sites

forest for the trees..... thanks

Share this post


Link to post
Share on other sites

Thanks guys, this is what i am using in case anyone else stumbles across this:

#include <Date.au3>

$date = @MON & "/" & @MDAY & "/" & @YEAR
If $date <> _lastWorkDay(@MON, @YEAR) Then
   $Run = "C:\Infor\Vmfg654\vmaplutl.exe"
   Run($Run)
EndIf

Func _lastWorkDay($iMonth, $iYear)
    Local $iLastWeekDay
    Local $iDaysInMonth = _DateDaysInMonth($iYear, $iMonth)
    Local $iWeekday = _DateToDayOfWeekISO($iYear, $iMonth, $iDaysInMonth)
    Switch $iWeekday
        Case 6, 7
            $iLastWeekDay = $iMonth & "/" & $iDaysInMonth - (($iWeekday = 7) ? 2 : 1) & "/" & $iYear
        Case Else
            $iLastWeekDay = $iMonth & "/" & $iDaysInMonth & "/" & $iYear
    EndSwitch
    Return $iLastWeekDay
EndFunc

 

Share this post


Link to post
Share on other sites

BrentFanGuy, this topic has interested me since I read the initial thread this morning.  I have put together some code that will also check for some holidays (based off the code I referenced upthread) if you're still interested.  I left the original check for Easter out because it adds a lot of lines to the code.  I have just been toying with this in my spare time today so there could be some errors in there, but it seems to be working fine for me.

 

#include <Date.au3>
#include <Array.au3>

$date = @MON & "/" & @MDAY & "/" & @YEAR
If $Date <> _lastWorkDay(@MON, @YEAR) AND _IsHoliday($Date) = False Then
   $Run = "C:\Infor\Vmfg654\vmaplutl.exe"
   Run($Run)
EndIf

Func _lastWorkDay($iMonth, $iYear)
    Local $iLastWeekDay
    Local $iDaysInMonth = _DateDaysInMonth($iYear, $iMonth)
    Local $iWeekday = _DateToDayOfWeekISO($iYear, $iMonth, $iDaysInMonth)
    Switch $iWeekday
        Case 6, 7
            $iLastWeekDay = $iMonth & "/" & $iDaysInMonth - (($iWeekday = 7) ? 2 : 1) & "/" & $iYear
        Case Else
            $iLastWeekDay = $iMonth & "/" & $iDaysInMonth & "/" & $iYear
    EndSwitch
    Return $iLastWeekDay
EndFunc

Func _IsHoliday($Date)
    $IsHoliday = False
    Local $Year = StringRight($Date, 4) ; get the year

    Dim $Thanksgiving ; Thanksgiving is always the 4th Thurs of November
    $NumofThurs = 0
    For $i = 1 to 30
        Local $iWeekday = _DateToDayOfWeek($Year, 11, $i)
            If $iWeekday = 5 Then ; (5) = Thursday
                $NumofThurs = $NumofThurs + 1
            EndIf
            If $NumofThurs = 4 Then
                $Thanksgiving = "11/" & $i & "/" & $Year
                ExitLoop
            EndIf
    Next
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Thanksgiving = ' & $Thanksgiving & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

    Dim $MemorialDay ; Last Monday in May
    For $i = 31 to 24 Step -1
        Local $iWeekday = _DateToDayOfWeek($Year, 5, $i)
            If $iWeekday = 2 Then ; (2) = Monday
                $MemorialDay = "05/" & $i & "/" & $Year
                ExitLoop
            EndIf
    Next
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $MemorialDay = ' & $MemorialDay & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

    Dim $LaborDay ; 1st Monday in Sept
    For $i = 1 to 7
        Local $iWeekday = _DateToDayOfWeek($Year, 9, $i)
            If $iWeekday = 2 Then ; (2) = Monday
                $i = StringRight("0" & $i,2)
                $LaborDay = "09/" & $i & "/" & $Year
                ExitLoop
            EndIf
    Next
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $LaborDay = ' & $LaborDay & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console


    Local $aHDay[1]
    _ArrayAdd($aHDay, "01/01/" & $Year) ; New Years
    _ArrayAdd($aHDay, "/12/25/" & $Year) ; Christmas Day
    _ArrayAdd($aHDay, $Thanksgiving) ; Thanksgiving
    _ArrayAdd($aHDay, $MemorialDay) ; Memorial Day
    _ArrayAdd($aHDay, $LaborDay) ; Memorial Day
    _ArrayAdd($aHDay, "12/30/2015") ; Every Day should be a holiday, right?
    ; add other national holidays here:

    For $i = 1 To UBound($aHDay) - 1
        If $Date = $aHDay[$i] Then
            $IsHoliday = True
            ExitLoop
        EndIf
    Next
    Return $IsHoliday
EndFunc   ;==>_IsHoliday

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now