cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
bakgou1
Helper I
Helper I

How to calculate dates between two dates selected by user?

Hi Community,

I don't know if this is doable in PowerApps. I am trying to calculate dates between two dates selected by user. For example.

If the user selects 01/28/2022 and 02/01/2022. I want to display 01/29/2022, 01/30/2022, 01/31/2022 in a text box.

 

Can this be achieved inside PowerApps? Thanks in advance.. Please help!

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@bakgou1 

Assuming you have two datepickers (dp1 and dp2), then your Default property on a Textbox would be:

With({_txt:
   Concat(Sequence(DateDiff(dp1.SelectedDate, dp2.SelectedDate, Days), 0), 
      Text(DateAdd(dp1.SelectedDate, Value, Days), ShortDate) & ", "
   )
},
   Left(_txt, Len(_txt)-2)
)

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

4 REPLIES 4
RandyHayes
Super User
Super User

@bakgou1 

Assuming you have two datepickers (dp1 and dp2), then your Default property on a Textbox would be:

With({_txt:
   Concat(Sequence(DateDiff(dp1.SelectedDate, dp2.SelectedDate, Days), 0), 
      Text(DateAdd(dp1.SelectedDate, Value, Days), ShortDate) & ", "
   )
},
   Left(_txt, Len(_txt)-2)
)

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
Digital
Super User
Super User

You use DateDiff and Date Add to make a collection of inbetween dates that you can display in a drop down.

 

Button.OnSelect

With(
    {
        locDateDiff: DateDiff(DatePicker1.SelectedDate,DatePicker2.SelectedDate)
    },
    ForAll(
        Sequence(locDateDiff),
        Collect(
        colInbetweenDates,
            {
                date:DateAdd(DatePicker1.SelectedDate,Value)
            }
        )
    )
)

 

Screen Shot 2022-02-02 at 11.41.33.png

 

bakgou1
Helper I
Helper I

@RandyHayes I appreciate your quick response. It worked like a gem! 😀

RandyHayes
Super User
Super User

@Digital 

No collection is needed for this for a text box.  Plus, your ForAll is backward.  You are using it like a ForLoop in some development language.

If you for some reason wanted this in an in-memory database in the app, then your formula would be written as:

ClearCollect(colInbetweenDates,
    ForAll(
        Sequence(DateDiff(DatePicker1.SelectedDate,DatePicker2.SelectedDate, Days), 0),
        {
            date: DateAdd(DatePicker1.SelectedDate, Value)
        }
    )    
)

Of course, this would have to then be done in a behavioral property ONLY.  So it would have to be initiated. 

 

ForAll is a function that returns a table.  It will do this despite how you use it.  So in your case, it is still generating a table with a duplicate of all the collection returned from the Collect function in each row of the sequence.  All of this is then put together as a table with (in your formula) a record with a single column called Value that will have the entire collection duplicated in the column for each row.

In this example, it is not extreme, but using a ForAll like that will, in larger examples, consume a lot of memory and resources to generate...and then you completely discard it when done in your formula.  So the process is wasted - but still performance is consumed.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (3,722)