gcue Posted March 29, 2023 Share Posted March 29, 2023 (edited) Thought I'd share this snippet I created based off jdelaney's post Calculates date in the past based off of only work days (ie: a week of just workdays). Any feedback welcome $start_date = "2023/03/27" $weekdays_ago = 2 $date = DateDiff_WeekDaysOnly($start_date, $weekdays_ago) Func DateDiff_WeekDaysOnly($start_date, $weekdays_ago) $weekdays = 0 $days_reduce = 0 While $weekdays <> $weekdays_ago $days_reduce -= 1 $running_date = _DateAdd("d", $days_reduce, $start_date) $aTemp = StringSplit($running_date, "/") $iDate = _DateToDayOfWeek($aTemp[1], $aTemp[2], $aTemp[3]) If $iDate <> 1 And $iDate <> 7 Then $weekdays += 1 EndIf WEnd $date_desired = _DateAdd("d", $days_reduce, $start_date) Return $date_desired EndFunc ;==>DateDiff_WeekDaysOnly Edited April 4, 2023 by gcue Skeletor and Gianni 2 Link to comment Share on other sites More sharing options...
Skeletor Posted March 31, 2023 Share Posted March 31, 2023 Nothing major but you can use $today = _NowCalcDate() instead: $today = _NowCalcDate() Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI Link to comment Share on other sites More sharing options...
gcue Posted April 1, 2023 Author Share Posted April 1, 2023 i had it like that originally. I just changed it to manual values so i can test with different dates thx Skeletor 1 Link to comment Share on other sites More sharing options...
Sascha Posted April 4, 2023 Share Posted April 4, 2023 Maybe this something for the "Autoit Snippets" thread? SOLVE-SMART 1 Link to comment Share on other sites More sharing options...
Gianni Posted April 4, 2023 Share Posted April 4, 2023 You could also implement the ability to calculate future dates, and also add another parameter to be able to indicate non-working days as you like (e.g. if someone works only Monday, Friday and Sunday he could pass '3,4,5,7' as holidays (assuming 1=Sunday, 2 =Monday , 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday) For fun, I wrote a function that has these possibilities using SQL (sqlite), but in order not to hijack your topic, for those interested, I post it in the session dedicated to SQL at this link: https://www.autoitscript.com/forum/topic/210024-a-workday-function-using-sqlite/ Skeletor 1 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
gcue Posted April 5, 2023 Author Share Posted April 5, 2023 very nice - hijack away Link to comment Share on other sites More sharing options...
Gianni Posted April 8, 2023 Share Posted April 8, 2023 (edited) another "hijack" here: https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1516466 p.s. ... sorry for intruding ... Edited April 8, 2023 by Gianni hijacked away Skeletor 1 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
Skeletor Posted April 8, 2023 Share Posted April 8, 2023 @Gianni I would not have realized these scripts exist. Thank you for posting the links. This will help alot. Gianni 1 Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI Link to comment Share on other sites More sharing options...
Nine Posted April 8, 2023 Share Posted April 8, 2023 (edited) Instead of counting every single days, it would be faster and somewhat more elegant to divide the number of working days into weeks and then only count the remaining few days : #include <Date.au3> Local $sDate = FastWorkDays(_NowDate(), -21) ConsoleWrite($sDate & @CRLF) Func FastWorkDays($sDate, $iNumDay) If Not $iNumDay Then Return SetError(1) Local $iSign = $iNumDay < 0 ? -1 : 1 Local $iNumWeek = Int(($iNumDay - $iSign) / 5) $iNumDay = Mod($iNumDay - $iSign, 5) + $iSign $sDate = _DateAdd("w", $iNumWeek, $sDate) Local $aTemp, $iDate While $iNumDay $sDate = _DateAdd("d", $iSign, $sDate) $aTemp = StringSplit($sDate, "/") $iDate = _DateToDayOfWeek($aTemp[1], $aTemp[2], $aTemp[3]) If $iDate = "1" Or $iDate = "7" Then ContinueLoop $iNumDay -= $iSign WEnd Return $sDate EndFunc Edited April 10, 2023 by Nine Gianni 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Gianni Posted April 10, 2023 Share Posted April 10, 2023 On 4/8/2023 at 2:20 PM, Nine said: Instead of counting every single days, it would be faster and somewhat more elegant to divide the number of working days into weeks and then only count the remaining few days : #include <Date.au3> Local $sDate = FastWorkDays(_NowDate(), -21) ConsoleWrite($sDate & @CRLF) Func FastWorkDays($sDate, $iNumDay) If Not $iNumDay Then Return SetError(1) Local $iSign = $iNumDay < 0 ? -1 : 1 Local $iNumWeek = Floor(($iNumDay - $iSign) / 5) $iNumDay = Mod($iNumDay - $iSign, 5) + $iSign $sDate = _DateAdd("w", $iNumWeek, $sDate) Local $aTemp, $iDate While $iNumDay $sDate = _DateAdd("d", $iSign, $sDate) $aTemp = StringSplit($sDate, "/") $iDate = _DateToDayOfWeek($aTemp[1], $aTemp[2], $aTemp[3]) If $iDate = "1" Or $iDate = "7" Then ContinueLoop $iNumDay -= $iSign WEnd Return $sDate EndFunc Hi @Nine if i'm not doing something wrong, there seems to be a bug there: tried with FastWorkDays('2023/04/10', -2) got 2023/03/30 instead of 2023/04/06 p.s. also, since _NowDate() is localized, better to use _NowCalcDate Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
Nine Posted April 10, 2023 Share Posted April 10, 2023 (edited) @Gianni You are right, Floor is not working as I expected on negative numbers. Thanks for testing (It seems I didn't test enough). Replacing it with Int solves the problem. Previous code corrected. Edited April 10, 2023 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Solution Gianni Posted April 11, 2023 Solution Share Posted April 11, 2023 Hi @Nine, It seems to work fine now, I used your calculation in this other post: https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1516574 bye, Thank you Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... 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