Alan1234 Posted October 18, 2017 Author Share Posted October 18, 2017 Malkey: Thanks for the example as it is much appreciated. After running the script I can now see that when a new month starts in the middle of the week it is counted as part of the last week number of the previous month. So now I can observe the error in numbering the weeks. What I am trying to accomplish is to identify the first Friday of each month. Perhaps I am going about solving this problem wrongly. Link to comment Share on other sites More sharing options...
Malkey Posted October 18, 2017 Share Posted October 18, 2017 Here is an example that finds the first specified day (Friday) of each month. #include <Date.au3> Local $iDayNumOfWeek = 6 ; The day number of the start of the week. (6 - Friday) For $j = 1 To 12 ConsoleWrite("1st " & _DateDayOfWeek($iDayNumOfWeek, 1) & ": " & @YEAR & "/" & $j & "/" & _DateFirstDayOfMonth($iDayNumOfWeek, @YEAR, $j) & @CRLF) Next Func _DateFirstDayOfMonth($FstFriMon = 6, $Year = @YEAR, $iMonth = @MON) For $i = 1 To _DateDaysInMonth($Year, $iMonth) If _DateToDayOfWeek($Year, $iMonth, $i) = $FstFriMon Then ExitLoop Next Return $i EndFunc ;==>_DateFirstDayOfMonth Skysnake 1 Link to comment Share on other sites More sharing options...
jchd Posted October 18, 2017 Share Posted October 18, 2017 Another way: Func _FirstFriday($iYear, $iMonth) Local $iDOW = _DateToDayOfWeek($iYear, $iMonth, 1) - 1 Return _DateAdd("D", ($iDOW = 6 ? 6 : 5 - $iDOW), StringFormat("%04i/%02i/01", $iYear, $iMonth)) EndFunc For $y = 2010 To 2020 For $m = 1 To 12 ConsoleWrite(_FirstFriday($y, $m) & @LF) Next Next Skysnake 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Deye Posted October 18, 2017 Share Posted October 18, 2017 just to see if i can understand this formula => $iDOW = 6 ? 6 : 5 - $iDOW How will it be done here: _First(2) ;monday Func _FirstDayOfTheWeek($iYear, $iMonth, $iDay) Local $iDOW = $iDay + 8 - _DateToDayOfWeek($iYear, $iMonth, 1) If $iDOW > 7 Then $iDOW -= 7 Return $iYear & "/" & $iMonth & "/" & $iDOW EndFunc Func _First($d) For $Y = 2017 To 2018 For $m = 1 To 12 ConsoleWrite(_DateTimeFormat(_FirstDayOfTheWeek($Y, $m, $d), 1) & @LF) Next Next EndFunc Link to comment Share on other sites More sharing options...
jchd Posted October 18, 2017 Share Posted October 18, 2017 The construct (ternary operator) lhs = condition ? true_expr : false_expr is a shorthand for If condition then lhs = true_expr else lhs = false_expr endif This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Malkey Posted October 18, 2017 Share Posted October 18, 2017 Another similar method. #include <Date.au3> For $y = 2010 To 2020 For $m = 1 To 12 ConsoleWrite(_DateOfFirstDayOfMonth(6, $y, $m) & @LF) ; 1 - Sunday, 6 - Friday, 7 - Saturday Next Next Func _DateOfFirstDayOfMonth($iDayNum, $iYear, $iMonth) Local $iDOW = _DateToDayOfWeek($iYear, $iMonth, 1) - 1 Return StringFormat("%04i/%02i/%02i", $iYear, $iMonth, Mod(7 + $iDayNum - $iDOW, 7) + ($iDayNum = $iDOW) * 7) EndFunc ;==>_DateOfFirstDayOfMonth Link to comment Share on other sites More sharing options...
Deye Posted October 21, 2017 Share Posted October 21, 2017 (edited) another example: This gets the day Instance # in a month by week number, when $bIODay = True, toggles from week number set to day instance number set #include <Date.au3> _DateDayOfWeekNumber(4, 4, 0) ; $iDayNum 1-7 , $iInst = Instance #, $bIODay = day instance number set Func _DateDayOfWeekNumber($iDayNum = @WDAY, $iInst = 1, $bIODay = False) Local $iWeekNum, $iInstance, $iDOW, $iDOW1, $iDOW2, $iDay, $sDate For $Y = 2017 To 2020 For $m = 1 To 12 $iWeekNum = $iInst $iInstance = $iInst $iFirst = _DateToDayOfWeek($Y, $m, 1) $iDOW = $iDayNum + 8 - $iFirst $iDOW1 = $iDayNum - $iDOW + 7 $iDOW2 = $iFirst - $iDayNum If $iDOW > 7 Then If ($iDOW1 ? 0 : $iDOW2) Then $iWeekNum += 1 $iDOW -= 7 Else If $iDOW2 <> $iDOW1 Then $iWeekNum += 1 EndIf If $bIODay = False And $iWeekNum > $iInst Then $iDOW -= 7 $iWeekNum -= 1 $iInstance -= 1 EndIf $iDay = $iDOW + ($iInst * 7) - 7 $sDate = $Y & "/" & $m & "/" & $iDay If _DateIsValid($sDate) Then ConsoleWrite($sDate & " Instance: " & $iInstance & " In week: " & $iWeekNum & @LF) EndIf Next Next EndFunc Edited October 22, 2017 by Deye Typo Link to comment Share on other sites More sharing options...
GPinzone Posted October 21, 2017 Share Posted October 21, 2017 Just curious. Do you need this code to calculate holidays? If so, there's a much easier way to handle things like calculating when the second Monday of October occurs. Gerard J. Pinzonegpinzone AT yahoo.com Link to comment Share on other sites More sharing options...
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