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!

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!

Anonymous
Not applicable

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
Super User 2 - 2022 Congratulations 768x460.png

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

<
Users online (2,994)