Sign in to follow this  
Followers 0
JDS

Get first Tuesday of the motnh

6 posts in this topic

I have a file copy script that needs to run once a month. I can have it run every 10th day... but then that day may not fall on a weekday. So how would I get the first weekday (say Tuesday) of the month?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I have a file copy script that needs to run once a month. I can have it run every 10th day... but then that day may not fall on a weekday. So how would I get the first weekday (say Tuesday) of the month?

I would look at @MDAY. Checking that the day is a Tuesday and that @MDAY is <= 7 should give you the first Tuesday of the Month.

Edited by Reaper HGN

Share this post


Link to post
Share on other sites

This should work for you.

#include <Date.au3>

$iYear = 2007
$iMonth = 08
$iDay = 3 ; Day number (1 = Sunday, 7 = Saturday).
$iInstance = 2 ; The instance of the day you want to return

$iReturn = _DateDayOfMonthInstance($iYear, $iMonth, $iDay, $iInstance)
If @error Then
    MsgBox(0, "Error", "The date you requested is invalid.")
EndIf
MsgBox(0, "", "The date you are looking for is " & $iMonth & "/" & $iReturn & "/" & $iYear)

Func _DateDayOfMonthInstance($iYear, $iMonth, $iDay, $iInstance)
    Local $i, $iCount, $iNumDays = _DateDaysInMonth($iYear, $iMonth)
    
    While $iCount < $iInstance
        $i += 1
        If $i > $iNumDays Then
            Return SetError(1, 0, 0)
        EndIf
        $iWeekday = _DateToDayOfWeek($iYear, $iMonth, $i)
        If $iWeekday = $iDay Then
            $iCount += 1
        EndIf
    WEnd
    Return SetError(0, 0, $i)
EndFunc   ;==>_DateDayOfMonthInstance

Enjoy,

Bob

Share this post


Link to post
Share on other sites

Or a simple GUI version...

#include <Date.au3>
#include <GuiCombo.au3>
#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("Day of Month Calculator", 463, 143, 193, 126)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$Button1 = GUICtrlCreateButton("GO", 335, 25, 116, 76, $BS_DEFPUSHBUTTON)
GUICtrlSetOnEvent(-1, "GO")
$Combo2 = GUICtrlCreateCombo("Select month...", 170, 25, 145, 25)
GUICtrlSetData(-1, "January|February|March|April|May|June|July|August|September|October|November|December")
$Combo3 = GUICtrlCreateCombo("Select day of week...", 10, 85, 145, 25)
GUICtrlSetData(-1, "Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday")
$Label1 = GUICtrlCreateLabel("Day instance (1-5)", 165, 60, 90, 17)
$Input2 = GUICtrlCreateInput("1", 170, 85, 153, 21)
$Updown2 = GUICtrlCreateUpdown($Input2)
GUICtrlSetLimit(-1, 5, 1)
$Label2 = GUICtrlCreateLabel('Provide the requested information and click "GO".', 0, 125, 462, 17, $SS_SUNKEN)
$Input1 = GUICtrlCreateInput("2007", 10, 25, 137, 21)
$Updown1 = GUICtrlCreateUpdown($Input1)
GUICtrlSetLimit(-1, 2999, 1)
$Label3 = GUICtrlCreateLabel("Year", 10, 5, 26, 17)
GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func GO()
    $iYear = Number(GUICtrlRead($Input1))
    $iMonth = _GUICtrlComboGetCurSel($Combo2)
    $iDay = _GUICtrlComboGetCurSel($Combo3)
    $iInstance = Number(GUICtrlRead($Input2))
    
    If $iMonth = 0 Or $iDay = 0 Then
        GUICtrlSetData($Label2, "You must provide all requested information.")
        Return
    EndIf

    $iReturn = _DateDayOfMonthInstance($iYear, $iMonth, $iDay, $iInstance)
    If @error Then
        GUICtrlSetData($Label2, "The date you requested is invalid.")
    Else
        GUICtrlSetData($Label2, "The date you are looking for is " & $iMonth & "/" & $iReturn & "/" & $iYear)
    EndIf
EndFunc   ;==>GO

Func _DateDayOfMonthInstance($iYear, $iMonth, $iDay, $iInstance)
    Local $i, $iCount, $iNumDays = _DateDaysInMonth($iYear, $iMonth)
    
    While $iCount < $iInstance
        $i += 1
        If $i > $iNumDays Then
            Return SetError(1, 0, 0)
        EndIf
        $iWeekday = _DateToDayOfWeek($iYear, $iMonth, $i)
        If $iWeekday = $iDay Then
            $iCount += 1
        EndIf
    WEnd
    Return SetError(0, 0, $i)
EndFunc   ;==>_DateDayOfMonthInstance

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Share this post


Link to post
Share on other sites

Thanks guys! :)

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
Sign in to follow this  
Followers 0