Jump to content

How Many Business Days Have Elapsed?


Recommended Posts

I am trying to read a edit field and tell whether or not 3 business days have elapsed. I have already accounted for people entering different ways of entering the date (4/12, 04/12, 4-12 etc..) but the problem I am having is trying to figure out how to calculate the number of business days that have passed from the entered date. I found a peice of VB code I was trying to modify but I cant seem to get it to work any help someone could provide would be appreciated. I was using msg boxes to try and debug.

HotKeySet("^!x", "MyExit")
HotKeySet("^b", "test")
#include <date.au3>

Func test()
    $SDate = datefunc("4/4/06")
    msgbox( 4096, "16" , "start date is: "& $SDate)
    $EDate = datefunc("04/12/06")
    msgbox( 4096, "18" , "end date is: "& $EDate)
    msgbox( 4096, "Business days", "Business days: " & getBusDays($SDate, $EDate))
EndFunc





Func getBusDays($SDate, $EDate)
    Dim $tmpDay
    $intGetBusDays = 0
    $SDate = _DateTimeFormat( $SDate, 2)
    msgbox( 4096, "30" , "start date is: "& $SDate)
    $EDate = _DateTimeFormat( $EDate, 2)
    msgbox( 4096, "32" , "end date is: "& $EDate)
    Do 
;~       tmpDay = Format($SDate, "w")
;~          $tmpDay = _DateTimeFormat( $SDate, 2)
        $parsedtext = StringLeft($SDate, 10)
        $array = stringsplit($parsedtext , ",./-\" , 0)
        $month = TwoDigits($array[1])
        $day = TwoDigits($array[2])
        $year = TwoDigits($array[3])
        $tmpDay = _DateToDayOfWeek($year , $month , $day)
        Select 
            Case $tmpDay = 2 
                $intGetBusDays = $intGetBusDays + 1
            Case $tmpDay = 3
                $intGetBusDays = $intGetBusDays + 1
            Case $tmpDay = 4
                $intGetBusDays = $intGetBusDays + 1
            Case $tmpDay = 5
                $intGetBusDays = $intGetBusDays + 1
            Case $tmpDay = 6
                $intGetBusDays = $intGetBusDays + 1
        EndSelect;»Select Case tmpDay
    $SDate = _DateAdd('d', 1, $SDate)
    Until $SDate = $EDate
;~ Loop '»Do Until SDate = EDate
msgbox( 4096, "Tab" , "business days is: "& $intGetBusDays)
Return $intGetBusDays
EndFunc

Func datefunc($dfInput)
;~  $text = ControlGetText("PeopleSoft CRM System -", "Problem Report ID", "Edit17")
    $parsedtext = StringLeft($dfInput, 10)
    $array = stringsplit($parsedtext , ",./-\" , 0)
    $month = TwoDigits($array[1])
    $day = TwoDigits($array[2])
    $count = count($array[3])
    If $count = 2 Then
        $year = "20" & TwoDigits($array[3])
    Else
        $year = $array[3]
    EndIf
    $date = $year & "/" & $month & "/" & $day & "[ 12:00:00]"
;MsgBox(4096, "String" , "First string:" & $array[1])
;MsgBox(4096, "String" , "First string:" & $array[2])
    MsgBox(4096, "String" , "Corrected date is: " & $date)
    Return $date
EndFunc

Func TwoDigits($input)
;~  If Not IsDeclared("countme") Then Dim $countme
;~  $countme = StringSplit($input, "")
    If count($input) = 2 Then;$countme[0] = 2 Then
        Return $input
    ElseIf count($input) = 1 Then;$countme[0] = 1 Then
        Return "0" & $input
    Else
        MsgBox(4096, "Error!" , "The Function TwoDigits did not function properly, check with the script coder.")
    EndIf
EndFunc

Func count($inputc)
    $arCount = stringsplit($inputc,"", 0)
    Return $arCount[0]
EndFunc

Func MyExit()
    Exit 
EndFunc 

While 1
    sleep(100)
WEnd
Link to comment
Share on other sites

Have you tried checking this functions: _DateDiff, _DateTimeFormat, _Now, _NowDate, _NowCalc, _NowCalcDate and bascially everything that starts with _Date (make sure you have beta autoit).

For example you could get today's date, enter the date you search (to make ppl not enter it by hand with lots of fuckups you could use GUICtrlCreateDate ( "text", left, top [, width [, height [, style [, exStyle]]]] ) to make them enter the date properly. Then you could just do -2 days for each week that passed. Well hope it helps.

Edited by MadBoy

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

Here is a function / example I wrote up for you.

_CalcBusinessDays takes two dates, and will return the value of business days for you.

Enjoy, - Simucal

#include <Date.au3>

Dim $SampleDate1="2006/04/01", $SampleDate2="2006/05/25", $r=1 ; will calculate the number of business days between $SampleDate1 and $SampleDate2

$BusinessDays = _CalcBusinessDays($SampleDate1, $SampleDate2)



Func _CalcBusinessDays($SampleDate1, $SampleDate2)
    $TempDate = $SampleDate1
    While $TempDate <> $SampleDate2
    $TempDateArray = StringSplit($TempDate, "/")
    $TempDateArray[1] = Number($TempDateArray[1])
    $TempDateArray[2] = Number($TempDateArray[2])
    $TempDateArray[3] = Number($TempDateArray[3])
        $DayOfWeek = _DateToDayOfWeek($TempDateArray[1],$TempDateArray[2],$TempDateArray[3])
        If $DayOfWeek = 1 Or $DayOfWeek = 7 Then
            $TempDate = _DateAdd("D",1,$TempDate)
        Else
            $r = $r + 1
            $TempDate = _DateAdd("D",1,$TempDate)
        EndIf
    WEnd
    msgbox(0,"Business Days","Number of business days is: " & $r)
    Return $r
EndFunc
AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)
Link to comment
Share on other sites

JdeB has refined the function I wrote and this works a lot faster.

Func _CalcBusinessDays($Date1, $Date2); Dates must be in format YYYY/MM/DD
    Local $TempDate,$dummy
    Local $Days = Abs(_DateDiff("d",$Date1, $Date2))                                                    
    Local $Weeks = Abs(_DateDiff("w",$Date1, $Date2))
    Local $Rest = $Days - ($Weeks * 7)
    Local $BDays = $Weeks * 5
    For $x = 1 To $rest
            _DateTimeSplit(_DateAdd("D",$x * -1,$Date2),$TempDate,$dummy)
            $DayOfWeek = _DateToDayOfWeek($TempDate[1],$TempDate[2],$TempDate[3])
            If Not ($DayOfWeek = 1 Or $DayOfWeek = 7) Then
                $BDays = $BDays + 1
            EndIf
    Next
    Return $BDays
EndFunc
Edited by Simucal
AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)
Link to comment
Share on other sites

JdeB has refined the function I wrote and this works a lot faster.

This is so great, thanks soo much!

Func _CalcBusinessDays($Date1, $Date2); Dates must be in format YYYY/MM/DD
    Local $TempDate,$dummy
    Local $Days = Abs(_DateDiff("d",$Date1, $Date2))                                                    
    Local $Weeks = Abs(_DateDiff("w",$Date1, $Date2))
    Local $Rest = $Days - ($Weeks * 7)
    Local $BDays = $Weeks * 5
    For $x = 1 To $rest
            _DateTimeSplit(_DateAdd("D",$x * -1,$Date2),$TempDate,$dummy)
            $DayOfWeek = _DateToDayOfWeek($TempDate[1],$TempDate[2],$TempDate[3])
            If Not ($DayOfWeek = 1 Or $DayOfWeek = 7) Then
                $BDays = $BDays + 1
            EndIf
    Next
    Return $BDays
EndFunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...