web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Difference between two...
Power Apps
Unanswered

Difference between two dates

(0) ShareShare
ReportReport
Posted on by 1,032

Hello everybody ;

I want to create an application that calculates the difference between two date times to calculate the number of employee absences as illustrated in the screenshot below, with the following management rules:

- Working hours are 8:30 a.m. to 1 p.m. and 2 p.m. to 6 p.m.
-Weekends not included.

How can I do this?

Capture d'écran 2023-10-03 154353.png
Categories:
  • v-bofeng-msft Profile Picture
    Microsoft Employee on at

    Hi @gikido ,

     

    Please try

    Set(StartDateTime,Date(2021,5,7)+Time(8,0,0));
    Set(EndDateTime,Date(2021,5,7)+Time(17,0,0));
    
    Sum(
    ForAll(
     Sequence(DateDiff(StartDateTime,EndDateTime,TimeUnit.Minutes)/30,1,1),
     With(
     {timeTemp:DateAdd(StartDateTime,Value*30,TimeUnit.Minutes)},
     If(
     Weekday(timeTemp)=1 || Weekday(timeTemp)=7 || Value(Text(Hour(timeTemp),"00")&Text(Minute(timeTemp),"00"))<=0830 || Hour(timeTemp)>18 || Hour(timeTemp)=13,
     {Value:0},
     {Value:0.5}
     )
     )
    ),Value)

    Bof

  • gikido Profile Picture
    1,032 on at

    @v-bofeng-msft  thank you for your reply 

     

    I tested your code and found errors in calculation cases for example:

     

    Set(StartDateTime;Date(2023;10;5)+Time(17;30;0));;
    Set(EndDateTime;Date(2023;10;7)+Time(18;0;0))

    gives: 10 while the exact result is 9

     

    Set(StartDateTime;Date(2023;10;5)+Time(17;30;0));;
    Set(EndDateTime;Date(2023;10;6)+Time(10;0;0))

    gives 2.5 while the exact result is 2

  • gikido Profile Picture
    1,032 on at

    @v-bofeng-msft 

    Hello ;

     

    are there any updates please?

     

    Thanks

  • v-bofeng-msft Profile Picture
    Microsoft Employee on at

    Hi @gikido ,

     

    Please try:

    Sum(
    ForAll(
     Sequence(DateDiff(StartDateTime,EndDateTime,TimeUnit.Minutes)/30,1,1),
     With(
     {timeTemp:DateAdd(StartDateTime,Value*30,TimeUnit.Minutes)},
     If(
     Weekday(timeTemp)=1 || Weekday(timeTemp)=7 || Value(Text(Hour(timeTemp),"00")&Text(Minute(timeTemp),"00"))<=0830 || Value(Text(Hour(timeTemp),"00")&Text(Minute(timeTemp),"00"))>1800 || (Value(Text(Hour(timeTemp),"00")&Text(Minute(timeTemp),"00"))>1300 && Value(Text(Hour(timeTemp),"00")&Text(Minute(timeTemp),"00"))<=1400),
     {Value:0},
     {Value:0.5}
     )
     )
    ),Value)

     

    Best Regards,

    Bof

  • gikido Profile Picture
    1,032 on at

    @v-bofeng-msft 

    thank you very much it's work now;

     

    Please I forgot a condition, it is that I have a table which contains holidays which I must eliminate and not count it as weekends; The table contains two columns "Id" and "DateDaysFier" of date type only.

    How can I integrate this condition into the function you provided me?

     

    THANKS

  • gikido Profile Picture
    1,032 on at

    @v-bofeng-msft 

     

    Hello ;

     

    are there any updates please for the last condition please? I'm stuck at this point.

     

    Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 402 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 328

#3
WarrenBelz Profile Picture

WarrenBelz 296 Most Valuable Professional

Last 30 days Overall leaderboard