#209316 - 2014-07-30 09:58 PM
Re: Get dates of a given week
[Re: Mart]
|
Mart
KiX Supporter
   
Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
|
Nevermind. I was thinking about this for some time and got totaly stuck and blind for the obvious.
The code below requires the WeekNumber() and DateCalc() UDFs.
Break on
$day1 = udfDatePicker()
;$day1 = "2014/07/28"
$day2 = DateCalc($day1, 1)
$day3 = DateCalc($day2, 1)
$day4 = DateCalc($day3, 1)
$day5 = DateCalc($day4, 1)
$week = WeekNumber(Join(Split($day1, "/"), "-"))
? "Week: " + $week
? "Day1: " + $day1
? "Day2: " + $day2
? "Day3: " + $day3
? "Day4: " + $day4
? "Day5: " + $day5
Sleep 5
; Function udfDatePicker()
;
; Inspired by Bruno's post here: http://www.kixforms.org/forum/viewtopic.php?t=672
;
; udfDatePicker() - Allow user to select a date from a calendar.
;
; Parameters: All parameters are optional.
; sTitle - Tool window title
; sStartday - The day that first appears in the tool
; iTodayButton - Does the "Today" button appear on the tool (true / false)
; Default is false
; $asSpecialDays - Array or space delimited list of special days which are
; highlighted in an alternate colour. Default is today.
; $sButtonStyle - How the individual dat buttons appear. Default is flat "popup"
; style, use "2" for a raised button effect.
; $sLowDay - The earliest day that can be selected.
; $sHighDay - The latest day that can be selected.
;
; Examples
; --------
; Display this month, highlight today
; $v=udfDatePicker()
;
; Only allow user to select my birthday :)
; $v=udfDatePicker("Special day","1966/12/21",,"1966/12/21",2,"1966/12/21","1966/12/21")
;
; Highlight the 10th and 20th this month, don't allow user to select days after today
; $v=udfDatePicker("","",1,""+@YEAR+"/"+@MONTHNO+"/10 "+@YEAR+"/"+@MONTHNO+"/20",,"",@DATE)
;
;
;
; $sMyBirthday=
; Amendement History
; ------------------
; 20091127 14:30 Richard Howarth <rhowarth@harsco.com>
; Function initially created.
;
Function udfDatePicker(
Optional $sTitle,
Optional $sStartDay,
Optional $iTodayButton,
Optional $asSpecialDays,
Optional $sButtonStyle,
Optional $sLowDay,
Optional $sHighDay
)
Dim $frm, $lblMonth, $btnPrev, $btnPrevYr, $btnNext, $btnNextYr, $btnToday
Dim $sDayHeader
Dim $s, $i
Dim $btnList[37]
Dim $iDaynoX, $iDaynoHeight, $iDaynoWidth
Dim $sAction
Dim $sToolColour, $sEvenDayColour, $sOddDayColour, $sTodayColour
Dim $iUpdate
Dim $iMonth, $iYear
Dim $iLastDay, $iFirstDay
Dim $aiDaysInMonth, $asMonthName
Dim $sThisDay
$sToolColour = LightGray
$sEvenDayColour = $sToolColour
$sOddDayColour = Silver
$sTodayColour = PaleGoldenrod
$aiDaysInMonth = x, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
$sDayHeader = "10:Mo 37:Tu 64:We 91:Th 118:Fr 140:Sa 166:Su"
$asMonthName = "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
$iDaynoX = 50 ; Offset for day
$iDaynoHeight = 20 ; Height of day button
$iDaynoWidth = 25 ; Width of day button
If $sLowDay = "" $sLowDay = "0000/00/00" EndIf
If $sHighDay = "" $sHighDay = "9999/99/99" EndIf
If VarType($asSpecialDays) & 8192 $asSpecialDays = Join($asSpecialDays) EndIf
If $asSpecialDays = "" $asSpecialDays = @DATE EndIf
$asSpecialDays = Split($asSpecialDays)
If $sStartDay = ""
$iMonth = CInt(@MONTHNO)
$iYear = CInt(@YEAR)
Else
$iMonth = CInt(Split($sStartDay, "/")[1])
$iYear = CInt(Split($sStartDay, "/")[0])
EndIf
$iUpdate = 1
If Not IsDeclared($_funDatePicker_KiXform)
Global $_funDatePicker_KiXform
EndIf
If VarType($_funDatePicker_KiXform) <> 9
$_funDatePicker_KiXform = CreateObject("KiXtart.System")
If @ERROR Exit @ERROR EndIf
EndIf
$frm = $_funDatePicker_KiXform.Form()
If @ERROR Exit @ERROR EndIf
;$frm.icon = $_funDatePicker_KiXform.BuiltinIcons(101)
$frm.formborderstyle = 1
$frm.maximizebox = 0
$frm.minimizebox = 0
$frm.width = 210
$frm.height = 215
$frm.text = "Select a date"
$lblMonth = $frm.Controls.Label()
$lblMonth.Bounds = 30, 10, $frm.width - 70, 18
$lblMonth.textalign = 32
$lblMonth.BackColor = $sToolColour
$lblMonth.BorderStyle = FixedSingle
$btnPrevYr = $frm.Controls.ToolButton()
$btnPrevYr.Icon = 27
$btnPrevYr.Left = 0
$btnPrevYr.height = 15
$btnPrevYr.width = 15
$btnPrevYr.top = 10
$btnPrevYr.flatstyle = 1
$btnPrevYr.onclick = "PreviousYr"
$btnPrev = $frm.Controls.ToolButton()
$btnPrev.Icon = 27
$btnPrev.Left = 15
$btnPrev.height = 15
$btnPrev.width = 15
$btnPrev.top = 10
$btnPrev.flatstyle = 1
$btnPrev.onclick = "Previous"
If $iTodayButton
$frm.height = 230
$btnToday = $frm.Controls.ToolButton()
$btnToday.text = "Today"
$btnToday.Left = ($frm.width /2) - 25
$btnToday.height = 20
$btnToday.width = 40
$btnToday.top = $frm.Height - 55
$btnToday.onclick = "Today"
$btnToday.BackColor = $sToolColour
$btnToday.FlatStyle = 1
EndIf
$btnNext = $frm.Controls.ToolButton()
$btnNext.Icon = 28
$btnNext.Left = $frm.width - 40
$btnNext.height = 15
$btnNext.width = 15
$btnNext.top = 10
$btnNext.onclick = "Next"
$btnNext.FlatStyle = 1
$btnNextYr = $frm.Controls.ToolButton()
$btnNextYr.Icon = 28
$btnNextYr.Left = $frm.width - 25
$btnNextYr.height = 15
$btnNextYr.width = 15
$btnNextYr.top = 10
$btnNextYr.flatstyle = 1
$btnNextYr.onclick = "NextYr"
; Create day header labels
For Each $s in Split($sDayHeader) $frm.PrintXY(Split($s, ":")[0], 30, Split($s, ":")[1]) Next
For $i = 0 to 36
$btnList[$i] = $frm.ToolButton()
$btnList[$i] .Location = 10 + $iDaynoWidth * ($i mod 7), $iDaynoX + $iDaynoHeight * ($i /7)
$btnList[$i] .width = $iDaynowidth
$btnList[$i] .height = $iDaynoHeight
$btnList[$i] .BorderStyle = 1
$btnList[$i] .FlatStyle = $sButtonStyle
Next
$labFrame = $frm.Controls.Label()
$labFrame.BorderStyle = 1
$labFrame.Text = ""
$labFrame.Width = 181
$labFrame.Height = 106
$labFrame.Top = 47
$labFrame.Left = 7
$frm.Show
While $frm.Visible
If $iUpdate
$lblMonth.Text = $asMonthName[$iMonth - 1] + " " + $iYear
; Fix leap day for this year
$aiDaysInMonth[2] = 28 + ((($iYear mod 4 = 0) And ( $iYear mod 100 > 0 )) Or ( $iYear mod 400 = 0 ))
; Calculate the offset for the first day of the month, algorithm taken from:
; http://www.faqs.org/faqs/calendars/faq/part1/index.html#ixzz0Y3svssjS
Dim $a, $y, $m
$a = (14 - $iMonth) /12
$y = $iYear - $a
$m = $iMonth + 12 * $a - 2
; The "1" here is hardcoded for the first day of the month
$iFirstDay = (1 + $y + $y /4 - $y /100 + $y /400 + (31 * $m) /12) mod 7
; Algorithm gives Sunday=0, fix date to ease calculation (Sunday=7)
$iFirstDay = $iFirstDay + 7 * ($iFirstDay = 0)
If $iFirstDay > 5
$labFrame.Height = 126
Else
$labFrame.Height = 106
EndIf
$btnPrev.enabled = TRUE
$btnPrevYr.enabled = TRUE
$btnNext.enabled = TRUE
$btnNextYr.enabled = TRUE
For $i = 0 to 36
If ($i + 1) >= $iFirstDay And ( $i - $iFirstDay + 1 ) < $aiDaysInMonth[$iMonth]
$btnList[$i] .Text = ($i + 2 - $iFirstDay)
$sThisDay = CStr($iYear) + "/" + Right("0" + $iMonth, 2) + "/" + Right("0" + $btnList[$i] .Text, 2)
$btnList[$i] .OnClick = 'btnDayNo ' + $sThisDay
$btnList[$i] .visible = TRUE
$btnList[$i] .Backcolor = IIf(($i mod 7) mod 2, $sOddDayColour, $sEvenDayColour)
If 1 + AScan($asSpecialDays, $sThisDay) And $sTodayColour $btnList[$i] .Backcolor = $sTodayColour EndIf
If $sThisDay < $sLowDay
$btnList[$i] .enabled = FALSE
$btnPrev.enabled = FALSE
$btnPrevYr.enabled = FALSE
Else
$btnList[$i] .enabled = TRUE
$btnPrev.enabled = TRUE
$btnPrevYr.enabled = TRUE
EndIf
If $sThisDay > $sHighDay
$btnList[$i] .enabled = FALSE
$btnNext.enabled = FALSE
$btnNextYr.enabled = FALSE
Else
$btnList[$i] .enabled = TRUE
$btnNext.enabled = TRUE
$btnNextYr.enabled = TRUE
EndIf
Else
$btnList[$i] .visible = FALSE
EndIf
Next
$iUpdate = 0
EndIf
$sAction = $frm.DoEvents
Select
Case $sAction = ""
; No action
Case $sAction = "Previous"
$iMonth = $iMonth - 1
If $iMonth < 1 $iMonth = 12 $iYear = $iYear - 1 EndIf
$iUpdate = 1
Case $sAction = "Next"
$iMonth = $iMonth + 1
If $iMonth > 12 $iMonth = 1 $iYear = $iYear + 1 EndIf
$iUpdate = 1
Case $sAction = "PreviousYr"
$iMonth = $iMonth - 12
If $iMonth < 1 $iMonth = 12 - Abs($iMonth) $iYear = $iYear - 1 EndIf
$iUpdate = 1
Case $sAction = "NextYr"
$iMonth = $iMonth + 12
If $iMonth > 12 $iMonth = Abs($iMonth) - 12 $iYear = $iYear + 1 EndIf
$iUpdate = 1
Case $sAction = "Today"
$iMonth = @MONTHNO
$iYear = @YEAR
$iUpdate = 1
Case Split($sAction)[0] = "btnDayNo"
$udfDatePicker = Split($sAction)[1]
$frm.hide
Case Default
"Unexpected event: " + $sAction + @CRLF
EndSelect
Loop
$frm = 0
Exit 0
EndFunction
_________________________
Mart
- Chuck Norris once sold ebay to ebay on ebay.
|
|
Top
|
|
|
|
#209328 - 2014-07-31 09:23 PM
Re: Get dates of a given week
[Re: Allen]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
|
OK - so if you are simply looking for the NEXT sequential Monday, my UDFs will handle that nicely. It will return a date in the format YYYY/MM/DD HH:MM:SS$aTarget = 0,1,'00:00:00' ; "NEXT", "MONDAY", "midnight (or any desired time)"
$Date = NextRunDate($aTarget)
'Date: ' $Date ? The example above returns the date of the next occurring Monday.
The NextRunDate takes an array with 3 elements. The first defines the "which Day", which can be next, first, second, third, fourth, or last. (numeric 0-5). The next element is the Weekday (1-7 for Mon-Sun, 0 for next), and the third element is the Time to run.
When using the "next" options, the time comes into play.. an array of 0,0,22:00 will return the next date that 10pm occurs - today if before 22:00 or tomorrow if it's after 22:00.
This UDF is used in two Kix-based services that schedule events to occur on my servers.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Henriques)
and 2516 anonymous users online.
|
|
|