Set canvas PowerApp date to the last day of the month

Set canvas PowerApp date to the last day of the month

 

When working with canvas PowerApps, you may have a need to have some date ranges set by default. A typical scenario might be where you have 2 date filters and you want to show records that occured between those date ranges. A scenario I had to work with recently was where the customer required the default filter dates to be the first day of the current month and the last day of the current month.

Setting a date to the first day of the month is relatively straight forward. Using a combination of the Date and the Now functions, you can set up your first day of the month variable as follows:

Set(varStart,Date(Year(Now()),Month(Now()),1))

The tricky part is getting the last day of the month. How do we know if its got 30 days or 31? What about the month of February?? What happens in a leap year!?
Turns out there is a handy undocumented “feature” when you use the Date function. If you set the day property to the number 0, it would return the last day of the previous month. So if I wanted to get the last day of February for example, my function would look as follows:

Set(varEnd,Date(2019,3,0))

Binding that variable to a date control I had, I was able to get the last day of February accurately!

Set canvas PowerApp date to the last day of the month

Hope this helps.