cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JanSomers91
Kudo Collector
Kudo Collector

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
CarlosFigueira
Power Apps
Power Apps

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!

View solution in original post

3 REPLIES 3
CarlosFigueira
Power Apps
Power Apps

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!

View solution in original post

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!

cosm1c
New Member

hello , I am beginner to powerapps.

i have a table in excel and i fetched it in power apps gallery .

the gallery has a some fields to show and a Datepicker.

 

the question is i want to show the same date in DatePicker as default date which was in excel sheet.

how should i do it?

datepicker problem powerapps .jpgdatepicker2.jpg

Helpful resources

Announcements
User Group Leader Meeting January 768x460.png

Calling all User Group Leaders!

Don't miss the User Group Leader meetings on January, 24th & 25th, 2022.

Power Apps Community Call Jan. 2022 768x460.png

Power Apps Community Call

Please join us on Wednesday, January 19th, at 8a PDT. Come and learn from our amazing speakers!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Top Solution Authors
Top Kudoed Authors
Users online (2,464)