Sign in to follow this  
Followers 0
gil900

i have a bug in my code and i don't know why..

4 posts in this topic

hello,

i worte a code that return the date range of the Previous Week

for example if the input is "18/8/2013-24/8/2013" (dates range of a week in the Month)

the the code shuld return: "11/8/2013-17/8/2013".

and this is working.

i also tested the input: "4/8/2013-10/8/2013" and the code return the right output: "28/7/2013-3/8/2013"

at this kind of inputs - when the anser shuld be with a Previous Month, my code works Almost Perfect.

but for some reson, there is a BUG when the input is this: "7/7/2013-13/7/2013"

the code return me this: "-6/7/2013"

the first date didn't created. anly the second date..

i don't know why..

i tryed to look hardly in my code and i didn't found what worng...

this is my code:

#include <Date.au3>

$test = ReturnPreDate("7/7/2013-13/7/2013")

;$test = _DateToDayOfWeek(2013, 8, -1)
;If $test
;MsgBox(0,"",$test)


MsgBox(0,"",$test)
ConsoleWrite($test&@CRLF)

Func ReturnPreDate($date)
    Local $output , $P_date1 , $P_date2
    $S_dates = StringSplit($date,"-",1)
    $S_date1 = StringSplit($S_dates[1],"/",1)
    $S_date2 = StringSplit($S_dates[2],"/",1)
    $P_day = $S_date1[1]-7
    If $P_day >= 1 Then
        $P_date1 = $P_day&"/"&$S_date1[2]&"/"&$S_date1[3]
        $P_date2 = $S_date1[1]-1&"/"&$S_date1[2]&"/"&$S_date1[3]
        $output = $P_date1&"-"&$P_date2
    Else
        Local $day = $S_date1[1] , $mon = $S_date1[2] , $yeer = $S_date1[3]
        $P_date2 = $day-1&"/"&$mon&"/"&$yeer
        For $a = 1 To 7
        $day = $day-1
        MsgBox(0,"",$day&" , "&$mon&" , "&$yeer)
        $var = _DateToDayOfWeek($yeer, $mon, $day)
        If $var = "" Then
            $day = 31
            If $mon-1 > 0 Then
                $mon = $mon-1
            Else
                $mon = 12
                $yeer = $yeer-1
            EndIf
            For $b = $day To $day-7 Step -1
                $var2 = _DateToDayOfWeek($yeer, $mon, $b)
                If $var2 = "" Then
                    $day = $day-1
                Else
                    ;$day = $b+1
                    ExitLoop
                EndIf
            Next
        ElseIf $var = 1 Then
            $P_date1 = $day&"/"&$mon&"/"&$yeer
            ExitLoop
        EndIf
        Next
    EndIf
    $output = $P_date1&"-"&$P_date2
    Return $output
EndFunc

Please help me figure out what is wrong!

thanks for helpers! :)

Share this post


Link to post
Share on other sites



How about this?

#include <Date.au3>
$sStartDate = "2013/08/18"
; Go back to start of week
While 1
    $aStartDate = StringSplit($sStartDate, "/")
    If _DateToDayOfWeek($aStartDate[1], $aStartDate[2], $aStartDate[3]) = 1 Then ExitLoop
    $sStartDate = _DateAdd("D", -1, $sStartDate)
WEnd
; Subtract a week
$sStartDate = _DateAdd("D", -7, $sStartDate)
$sEndDate = _DateAdd("D", 6, $sStartDate)
ConsoleWrite($sStartDate & " - " & $sEndDate & @LF)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

 

How about this?

#include <Date.au3>
$sStartDate = "2013/08/18"
; Go back to start of week
While 1
    $aStartDate = StringSplit($sStartDate, "/")
    If _DateToDayOfWeek($aStartDate[1], $aStartDate[2], $aStartDate[3]) = 1 Then ExitLoop
    $sStartDate = _DateAdd("D", -1, $sStartDate)
WEnd
; Subtract a week
$sStartDate = _DateAdd("D", -7, $sStartDate)
$sEndDate = _DateAdd("D", 6, $sStartDate)
ConsoleWrite($sStartDate & " - " & $sEndDate & @LF)

 

It's great! (With some adjustments)

You used the correct shortcuts .. I was supposed to use _DateAdd ()

Thank you!

In any case,

If you want you can try to figure out where the problem in my code ..

I would love to know what is wrong. But I can do without it.

Share this post


Link to post
Share on other sites

It's great! (With some adjustments)

I knew that some adjustments would be needed ;)

Just wanted to show you a short example how to do it.

I think your code is overyl complex. Let _DateAdd do all the calculation. Only use the start date because the enddate is not relevant as you always return just a week.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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