Page 1 of 1 1
Topic Options
#209313 - 2014-07-30 09:10 PM Get dates of a given week
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Question.

I'm creating a small app that allows certain users to start a process for for other users on the days in a given week. They specify the week number and then I would like to get the first five dates of that week. Only working days are needed but if it includes the weekend I can work around that. I'm working with a week starting on Monday in this case.

Sure I can get the week number using the Weeknumber() UDF like below but is there a way to get the dates belonging to that week? E.g. if I specify week 31 of this year I would like to have July 28th, 29th, 30th, 31st and August 1st.

I really s#ck at date math in scripts so any help is greatly appreciated.

 Code:
Break on

Weeknumber()

Sleep 5


Edited by Mart (2014-07-30 09:42 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#209315 - 2014-07-30 09:45 PM Re: Get dates of a given week [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Hmmmm......was just thinking about it and why not let the user specify the date and then calculate the next four days and the weeknumber from that point on. Doing some testing now.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#209316 - 2014-07-30 09:58 PM Re: Get dates of a given week [Re: Mart]
Mart Moderator Offline
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.

 Code:
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
#209317 - 2014-07-30 10:04 PM Re: Get dates of a given week [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Mart,

The NextRunDate() UDF on my site (along with the two dependent UDFs) might give you what you need. I use it for "Third Saturday at 15:00" or "last Wednesday at 12:00 type calculations.

With a minor tweak to allow past dates, you could request the "First Monday of January" and then it's an easy calculation to the 31st Monday (ignoring a short week) or the 30th week if you want to include any short week. "Short Week" might be defined as the first Monday being a date greater than the 3rd.

The UDF download contains two UDFs, actually, and you'll need the TimeConvert() and TimeDiff() UDFs as well. The mod to specify a date should not be difficult - add an optional arg and if present/defined, use that instead of the @DATE macros to determine the desired starting date.

I might be able to assist if you think these UDFs would help.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#209318 - 2014-07-30 10:30 PM Re: Get dates of a given week [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Hi Glenn.
Thanks for the tip. I think I should be a bit more clear about the project. It is actually an attendance application that allows people to set their attendance for the coming week. Usually on Wednesday one specific user (let’s call it the attendance admin) manually prepares an Excel sheet and sends and e-mail to all employees in a specific office asking them to put their attendance in the Excel sheet for the coming week. This works but kinda sucks because it has too many user actions and uses something that was actually not intended for that purpose.

What I am building is an application that allows the attendance admin to prepare a new week and create empty entries in an SQL database for all users in the selected office. The users use the same application (with the admin part disabled depending on group membership) to enter their data and basically update their entries in the database. The "create new week"-process needs to know the dates of the week to be created to be able to create unique entries for the dates for each employee.
What I have now works but your UDFs may also do the trick and I'll certainly give them a go.

Thanks again.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#209319 - 2014-07-31 01:59 PM Re: Get dates of a given week [Re: Mart]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
If we are going forward in weeks alone, calculating next Monday from current weekday is not hard. Week number gives you current week. Hmm. So what you actually kind of want is nextmonday() that optionally takes a day as an input....
_________________________
!

download KiXnet

Top
#209320 - 2014-07-31 02:16 PM Re: Get dates of a given week [Re: Lonkero]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Yes. What I want is next Monday indeed and then calculate Tuesday, Wednesday, Thursday and Friday of that same week.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#209321 - 2014-07-31 02:38 PM Re: Get dates of a given week [Re: Mart]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
That EndofMonth () golf challenge would be a good starting point.

Edit... or maybe not... can't make head or tails of the math anymore...
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=203324#Post203324


Edited by Allen (2014-07-31 02:50 PM)
_________________________
(... better days ahead)

Top
#209322 - 2014-07-31 03:06 PM Re: Get dates of a given week [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
Using SerialDate

 Code:
$nextmonday=7-@wdayno + 1 + serialdate(@date)
for $i=$nextmonday to $nextmonday+4
  ? serialdate($i)
next
_________________________
(... better days ahead)

Top
#209328 - 2014-07-31 09:23 PM Re: Get dates of a given week [Re: Allen]
Glenn Barnas Administrator Offline
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
 Code:
$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! \:D

Top
#209331 - 2014-08-01 09:19 AM Re: Get dates of a given week [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Thanks for all the suggestions guys. I'm working a different part of the application right now but when that is done I'll come back to this and see what works best for us.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.072 seconds in which 0.033 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org