Here's a simple script I wrote to get the dates of a specified no. of Sundays starting from any specified date. I also wrote a function to give the list of dates an organized look: DatesOrganizer().
Code:
; Finds a specified no. of Sundays starting from a specified date
#NoEnv
#SingleInstance, force
#Include DateParse.ahk
; Date parser - convert any date format to YYYYMMDDHH24MISS by Titan
; Topic : www.autohotkey.com/forum/viewtopic.php?t=20405
; Download : http://www.autohotkey.net/~Titan/repos/trunk/DateParse.ahk
Gui, Margin, 1, 1
Gui, Font, s9 w1000, Courier New
Gui, Add, Text, x6 w70 h20, Look from:
Gui, Add, DateTime, x+5 hp w95 vstartDate gcalculate, dd/MM/yyyy
Gui, Add, Text, x+5 w20 hp, for
Gui, Add, Edit, x+5 w45 hp Number Right gcalculate ; typing directly into this edit control has never caused trouble
Gui, Add, UpDown, 0x2 0x80 Range0-366 vreqDays gcalculate ; using this UpDown control is causing trouble
Gui, Add, Text, x+5 w50 hp, Sundays
Gui, Add, Edit, xm w300 r15 ReadOnly -TabStop vdisplay,
xPos := A_ScreenWidth - 350
Gui, show, x%xPos% AutoSize, Sunday Finder
Gosub, calculate
Return
GuiEscape:
GuiClose:
ExitApp
calculate:
Gui, Submit, NoHide
sunday = 0 ; sunday counter
sundayDt = ; aims to generate a list of comma separated sundays
testDate := startDate ; assign startDate to a variable before starting calculation
While, sunday < reqDays ; do it while the sunday count is less than required number
{
FormatTime, hDay, %testDate%, WDay ; get the day of week of testDate
If (hDay = 1) ; test if it's sunday
{
sunday++ ; increase sunday counter
FormatTime, sDt, %testDate%, dd/MM/yyyy ; format sunday date to regular format
sundayDt = %sundayDt%%sDt%`, ; and generate a comma separated string of such sundays
}
testDate += 1, Days ; get ready to test next date
}
StringTrimRight, sundayDt, sundayDt, 1 ; remove the last comma, or DatesOrganizer() func. adds an extra first day
sundayDt := DatesOrganizer(sundayDt) ; comment this line on a long list of dates to see the HUGE difference it makes
GuiControl,, display, No. of Sundays: %sunday%`n%sundayDt%
Return
/*
Function: DatesOrganizer
Takes a (comma separated dates list) as input and returns output after giving them an organized look
Note that for an orgnized look, the list of dates given as input should already be sorted - it's almost always prepared that way.
Parameter:
_datesList - a comma separated list of dates to be parsed for an organized look
Example:
organizedList := DatesOrganizer(datesList)
; where the input may contain the list of Govt. holidays, for example: datesList=5/1/10,14/1/10,20/1/10,23/1/10,26/1/10,30/1/10,5/2/10,12/2/10,27/2/10,1/3/10,18/3/10,24/3/10,2/4/10,14/4/10,23/4/10,1/5/10,27/5/10,30/6/10,13/7/10,2/9/10,11/9/10,18/9/10,2/10/10,8/10/10,14/10/10,15/10/10,16/10/10,5/11/10,11/11/10,12/11/10,15/11/10,17/11/10,17/12/10,25/12/10
Include file used: DateParse.ahk ; Thanks to Titan http://www.autohotkey.com/forum/topic20405.html
*/
DatesOrganizer(_datesList)
{
organizedList = ; the organized list of dates will be created in this variable
yearMonthPrevious =
Loop, parse, _datesList, `, ; parse the comma-separated list of dates
{
testDay := DateParse(A_LoopField) ; 20100105 [for example]
StringLeft, yearMonth, testDay, 6 ; separate year and month in yyyyMM format - in this example: 201001
If (yearMonth = yearMonthPrevious) ; if same (month AND year) as previous testDay
{
FormatTime, dt, %testDay%, dd ; get only the date part
organizedList = %organizedList%`,%dt% ; and comma-append it to existing string
}
Else ; if new (month OR year)
{
yearMonthPrevious = %yearMonth% ; this will be used in next comparison
FormatTime, yrMn, %testDay%, yyyy`,%A_Space%MMM ; get the formatted year and month
FormatTime, dt, %testDay%, dd ; and also the formatted date
organizedList = %organizedList%`n%yrMn%:%A_Space%%dt% ; the FIRST newline character must be removed when organizedList has been fully prepared
}
}
StringReplace, organizedList, organizedList, `n ; remove the newline character at the start of fully prepared organizedList
Return, organizedList
}
Here's the trouble:
If I use the UpDown control to increase/decrease the number of Sundays needed (either with the arrow keys of keyboard or clicking the arrow buttons of the control), the display garbles (always by the current date) up at irregular intervals. Sometimes it will garble up on 6 while going up, other times at 23 or 25 or whatever. While coming down, it will garble up at a different no.! And usually, if it gives a confused display at a particular no. while going up, it will give a perfect display at that no. while coming down! Do it again and it will give the confused display at a different no.! Just focus the UpDown control and repeatedly press the Up arrow on the keyboard to see what I mean. First go up to say, 50 then come down. Repeat to see confused display at different numbers.
Till now the script has run flawlessly:
1. if I type directly the required numbers of Sundays into the edit control and not use the UpDown control
2. if I don't use the DatesOrganizer() function - but this function does make a huge difference in display, so I want to use it. I tried but could not find any error in it.
For now, the only working solution I can think of is to remove the UpDown control and assign the variable reqDays to the Edit control instead of UpDown.
I've tried to document the script well hoping that it will be easier to understand. Can anyone please look into the problem and help me find a way to use the UpDown control? Thanks.