This is a typical post on a Friday afternoon, about weird date handling in Power Apps. But understanding this issue will help in many situations.

Dates in Power Apps

A couple of examples of using dates in Power Apps

Now()
"2024-12-31"   // huh this is not a date it is a text with a date or maybe not??

Formatting of dates in If statement

Now consider the following code in a label in Power Apps.

If( false,
    Now(),
    "2024-12-31"
)

What will this return?

Weird date handling in Power Apps
Weird date handling in Power Apps

A bit of a surprise here!

Why are the 00:00 added to my date?

If we format the true part of the If logic then get a different behaviour:

Weird date handling in Power Apps Fixed
Weird date handling in Power Apps Fixed

Now why is this?

Explanation of date issues

In the first example:

If( false,
    Now(),
    "2024-12-31"
)

Here the true branch sets the type of data to Date and Time, and then the false branch will auto convert to Date and Time.

In our second example the true branch is setting the output of the if to text and therefor the false branch will just return the given text.

I hope this helps someone else.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

2 thoughts on “Weird date handling in Power Apps

  1. Hi Pieter
    It’s funny you mention weird datetime behaviour, cause I have this code that has been working for a long time and all of a sudden, it failed. The code just takes the date and time Now(), replaces minutes and seconds with 00:00, so the start of the hour and an other one does the same and add one hour and stores it into a table.

    Here’s the code:
    Set(TimeNow, DateTimeValue($”{Text(First(Split(Text(Now(),DateTimeZone.UTC),”:”)).Value)}:00:00.00Z”));
    Set(TimeOneHour, DateTimeValue($”{Text(First(Split(Text(DateAdd(Now(),1,TimeUnit.Hours),DateTimeZone.UTC),”:”)).Value)}:00:00.00Z”));
    Set(varTimeRegItem, Patch(‘Time Registrations’,Defaults(‘Time Registrations’),{Task:ThisItem},{‘Invoice description’:ThisItem.Subject},{‘Start time’:TimeNow},{‘End time’:TimeOneHour},{‘Worked Hours’:1.0},{‘Invoice hours’:1.0}));

    It can problably be done in a smarter way:)

    The weird part is that it stopped working one Tuesday telling me that “2024-11-26T17:00:00.00Z” was not a valid date. I tried everything, gave up and the next day it worked again…

    My guess is that some current MS update to our environment temporarily messed up the date time format and made an update and then it worked again.

    But yes, for sure, date time handling can be weird

    1. Hi Jesper,

      Rather than concatenating the time to the date I would use formatstrings instead.

      Then for the Patch function it looks like you are updating each field as a separate object. I would do that as a single object merging the Invoice description , Start time, End time, Worked Hours and Invoice hours. That would simplify the code quite a bit.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.