Blog
Datetime - Preventing user manipulation of the current time - how to retrieve a server date time value
November 26. 2024
If you want to retrieve the current date and time in a way that can't be manipulated by the user changing the device time, here's a technique you can use.
When building apps, it's easy to retrieve the current date and time by calling the Now() function.
A problem with calling Now() is that it returns the client datetime, and end
users can manipulate this by changing the time on the device.
When building apps where we want to enforce time limits on how long a user spends carrying out a task, this behaviour can allow users to circumvent the time limits that we want to impose.
There's no built-in function to retrieve the server time, however, we can derive the server time by writing a record to SharePoint. This post walks through this process.
How to set up a SharePoint list to retrieve the current datetime
For each record in a SharePoint list, SharePoint adds a server-generated create and 'last modified' dates and times. Therefore, we can write a record to SharePoint and retrieve the last modified date and time.
How to set up a SharePoint list to retrieve the current datetime
For each record in a SharePoint list, SharePoint adds a server-generated create and 'last modified' dates and times. Therefore, we can write a record to SharePoint and retrieve the last modified date and time.SharePoint is a good data source because unlike using a custom connector or Dataverse, the use of this data source is covered under standard licensing.
For this demonstration, we'll create a SharePoint list called AppHelper. This list contains only the Title column, and the server-generated columns, including the Modified column.
How to get the Server Time from Power Apps
With our SharePoint list setup, here's the formula to Patch a record and retrieve the 'modified' datetime of the record.
Set(varServerDateTime,
Patch(AppHelper,
Coalesce(First(AppHelper), Defaults(AppHelper)),
{Title:"ServerDate"}
).Modified
)
We can place this formula in the OnStart property of a screen or some other suitable place.
This formula updates the first record in the SharePoint list or creates a record if it doesn't exist. It saves the return value in a variable called varServerDateTime. We can then enter varServerDateTime whenever we want to use the server date time.
To refresh the value, we can simply call the above formula again.
Summary
To retrieve the current time server-side, we can create a record in SharePoint and retrieve the server-generated 'last modified' date and time. This ensures a time value that remains unaffected by any changes the end user might make to the device time.
- Categories:
- datetime
Related posts
- Dates - How to display minutes as hours/minutes, and days/hours/minutes
- Formula - How to round times to the nearest x minutes
- Dates - How to convert dates to Islamic Hijri format
- Data - How to filter records by a specific date, today's date, or range of dates
- Formulas - how to return all days or working days in a month
- Dataverse - How to calculate durations with calculated columns