Hi folks,
I am wanting to set a date picker according to the week number selected.
I am able to get the week number from the date but struggling on how to go about this in reverse.
I would like to select the first day of a week, depending on the week number, in a date picker component but also, only going back in date.
For example it is week 14 of the year and I want to select week 51 of the previous year. Upon selection of week 51 the date picker changes to show the first day of week 51 in the previous year not current year?
Any ideas on how this would be possible
Kind Regards
Richard
With scripting or expression?
I'd just create a week selector popup.

Try opening the file below and copy and paste the code into your project on Project Browser | Perspective | Views.
weekPicker.txt (100.9 KB)
Notes:
- The popup has two parameters, year and week. These can passed from the calling routine and used as required. The year parameter is used in the banner heading.
- The button has a custom property
week
with an expression binding to extract the week number from the button's name, "btnWeek1", etc.
subString({this.meta.name}, 7)
This means that we can define button 1 exactly the way we want it, then copy and paste it 51 times into the flex row container (with wrap : wrap
enabled).
- The button has an event to call a script, etc.
- Define a style class for the button and apply that to button 1. Then you can change the appearance of all 52 with one style edit.
1 Like
Thanks for your reply @Transistor
I haven't explained what I am trying to do very well.
I have a date picker and a dropdown box.
The dropdown box is a list of 52 weeks.
I would like the user to be able to pick a weeknumber or a date.
If the user selects a date the dropdown box will display the week for that date
but the problem I have is if the user picks a weeknumber from the dropdown box, is getting the relevant date to display, the first day of that week, in the datepicker.
Hope this makes sense 
So, I think I need to find a way to take the week selected in the dropdown box and convert this to the first day of that week in the datepicker.
Will this always be the current year, or is it necessary to use the year that is currently selected in the date picker?
Hi @justinedwards.jle
They should only be able to go back in time, so it is currently week 14 of this year but if they need to edit week 51, that would be the previous year week 51.
This function will get you close to what you want without a bunch of convolution:
# Gets the aproximate date of a given week number and year
def getDateFromWeek(weekNumber, year):
# Set the starting date to January 1st of a given year
startingDate = system.date.parse('January 01, {}'.format(year), 'MMMM dd, yyyy')
# Add the number of weeks to the date (minus one to make it zero indexed) and return it
return system.date.addWeeks(startingDate, weekNumber - 1)
# Example call
selectedDate = getDateFromWeek(14, 2024)
print selectedDate
Output:
Mon Apr 01 00:00:00 CDT 2024
Use what you already know to calculate what year to use, and simply pass it into the function along with the week number.
1 Like
Thanks @justinedwards.jle this worked a treat, as you said this got me close.
Added an if statement so any week higher than the current week would return the week in the previous year.
I just need to fine tune it to return the correct day of the selected week if it is in the previous year.
def getDateFromWeek(WeekNumber, Year):
# Set the starting date to January 1st of a given year
date = system.date.now()
year = system.date.getYear(date)
startingDate = system.date.parse('January 01, {}'.format(year1), 'MMMM dd, yyyy')
# Add the number of weeks to the date (minus one to make it zero indexed) and return it
return system.date.addWeeks(startingDate, WeekNumber - 1)
# Example call
date = system.date.now()
year = system.date.getYear(date)
currentWeek = self.getSibling("Label_4").props.text
if self.getSibling("Dropdown").props.value > currentWeek:
year1 = (year - 1)
else:
year1 = year
selectedDate = getDateFromWeek(self.getSibling("Dropdown").props.value, year1)
self.getSibling("DateTimePicker").props.value = selectedDate
1 Like