cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JanSomers91
Level 8

DatePicker Change Date Programmatically

Hello,

 

I have 2 buttons (next and previous) and a datepicker control.

The datepicker is defaulted to today. 

 

I want te be able to jump to the datepickers next day by clicking next and vice versa.
How do I do this?

1 ACCEPTED SOLUTION

Accepted Solutions
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: DatePicker Change Date Programmatically

You can use a global variable to store the date that is bound to the Default property in the date picker, and in the prev/next buttons you'd decrement/increment the variable value.


In the app's OnStart property (you can find it on the first screen of your app), you'd need to initialize that value:

<first screen>.OnStart: Set(defaultDate, Today())

Now you can add the two buttons (prev/next) and set their properties to update the value of that property:

Button1.Text: "Previous day"
Button1.OnSelect: Set(defaultDate, DateAdd(defaultDate, -1, Days))
Button2.Text: "Next day"
Button2.Text: Set(defaultDate, DateAdd(defaultDate, 1, Days))

In the date picker, you then set its Default property to the "defaultDate" variable, so that when that property is changed (by clicking the previous/next buttons), then the date displayed in the picker will be changed as well.

DatePicker1.DefaultDate: defaultDate

The last thing you need to do is to update the "defaultDate" variable when the user selects a new date in the picker's calendar (so that the variable reflects what the date picker is showing, and incrementing / decrementing the date will produce the correct result):

DatePicker1.OnChange: Set(defaultDate, DatePicker1.SelectedDate)

Hope this helps!

2 REPLIES 2
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: DatePicker Change Date Programmatically

You can use a global variable to store the date that is bound to the Default property in the date picker, and in the prev/next buttons you'd decrement/increment the variable value.


In the app's OnStart property (you can find it on the first screen of your app), you'd need to initialize that value:

<first screen>.OnStart: Set(defaultDate, Today())

Now you can add the two buttons (prev/next) and set their properties to update the value of that property:

Button1.Text: "Previous day"
Button1.OnSelect: Set(defaultDate, DateAdd(defaultDate, -1, Days))
Button2.Text: "Next day"
Button2.Text: Set(defaultDate, DateAdd(defaultDate, 1, Days))

In the date picker, you then set its Default property to the "defaultDate" variable, so that when that property is changed (by clicking the previous/next buttons), then the date displayed in the picker will be changed as well.

DatePicker1.DefaultDate: defaultDate

The last thing you need to do is to update the "defaultDate" variable when the user selects a new date in the picker's calendar (so that the variable reflects what the date picker is showing, and incrementing / decrementing the date will produce the correct result):

DatePicker1.OnChange: Set(defaultDate, DatePicker1.SelectedDate)

Hope this helps!

Highlighted
JanSomers91
Level 8

Re: DatePicker Change Date Programmatically

Works like a charm, it was a bit buggy at first but a clean restart fixed all settings!

Thanks a lot for the quick and helpful response!

Helpful resources

Announcements
firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

SecondImage

PowerApps Monthly Community Call

Tomorrow, September 18th at 8am PDT

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

SecondImage

Power Platform Summit North America

Register by September 5 to save $200

Top Kudoed Authors
Users Online
Currently online: 297 members 5,076 guests
Please welcome our newest community members: