Guest Posted August 3, 2013 Posted August 3, 2013 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: expandcollapse popup#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!
water Posted August 3, 2013 Posted August 3, 2013 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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Guest Posted August 3, 2013 Posted August 3, 2013 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.
water Posted August 3, 2013 Posted August 3, 2013 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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now