Thursday, June 3, 2021

DateTime Issue In PowerApps

 Welcome back with another post on PowerApps. We all face DateTime issue when using PowerApps with Power Automate and SharePoint. The reason is that time zone of all these portal is different due to which when we send DateTime value from PowerApps to SharePoint, it gets changed according to the time zone.

The possible solution provided by PowerApps is to set Date time zone to Local as shown in screen below-



But it also not works well. See how-

I had created a list having 2 columns-

  1. Title - Single line of text
  2. WorkDate - DateTime (include time also)
Now, I had created a simple PowerApp using SahrePoint default feature Integrate >> PowerApps >> Create an app. Now, I submitted one record from PowerApp to SharePoint as below-



Clearly, there is a difference in both datetime even I had set the Date time zone to Local.

Therefore, I came up with another workaround. Suppose we have to capture InDateTime. Let's see-

  1. Instead of creating a DateTime column, create 2 columns-
    1. "InDate" - Single line of text
    2. "InTime" - Single line of text
  2. Now in PowerApps, use Date picker as date column "dpInDateEdit". Set Date time zone to Local.


  3. Use drop down to create Hours (ddlInTimeHoursEdit) & Minutes (ddlInTimeMinutesEdit).

  4. This way you can create your own DateTime picker.
  5. Now the question is how to bind current value on screen load in New Entry Form and the default value in Edit Form. For this- 
  6. New Form -
    1. Choose the DefaultDate property of "dpInDateNew" control and set it to -
      1. Today()
        
    2. Choose the Default property of "ddlInTimeHoursNew" control and set it to -
      1. If(Hour(Now()) < 10,Concatenate("0",Text(Hour(Now()))),Hour(Now()))
        
    3. Choose the Default property of "ddlInTimeMinutesNew" control and set it to -
      1. If(Minute(Now()) < 10,Concatenate("0",Text(Minute(Now()))),Minute(Now()))
        
  7. Edit Form - 
    1. Choose the DefaultDate property of "dpInDateEdit" control and set it to -
      1. BrowseGalleryListItems.Selected.InDate
    2. Choose the Default property of "ddlInTimeHoursEdit" control and set it to -
      1. First(Split(BrowseGalleryListItems.Selected.InTime,":")).Result
    3. Choose the Default property of "ddlInTimeMinutesEdit" control and set it to -
      1. Last(Split(BrowseGalleryListItems.Selected.InTime,":")).Result
  8. You will be surprising that why I am splitting the time. The reason is that while saving the data from New/Edit form to SharePoint, I am concatenating the Hours & Minutes value to save in list.
    1. UpdateContext({varInTime:Concatenate(ddlInTimeHoursEdit.SelectedText.Value,":",ddlInTimeMinutesEdit.SelectedText.Value)});
      
  9. Before saving the date, I am changing it to DatePicker format so that next time, when I will bind it to this control on Edit Form, it will create any issue-
    1. UpdateContext({varInDate:Text(dpInDateEdit.SelectedDate,"mmmm d, yyyy")});
      
  10. This way, we can resolve the DateTime issue of timezone.
  11. In case, if you wish to save the complete datetime in one column, you may concatenate all 3 values as per the required format and save in single column.
  12. While fetching, you need to split the same and fetch the desired value from the array accordingly.
  13. That's all for this topic.


With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.