Enforce datetime value format in Power apps
I need to save the date time value in MS SQL server. The format for datatype “Date” and “Datetime” are:
DATE
- YYYY-MM-DDDATETIME
- YYYY-MM-DD HH:MI:SS
The problem is, since Power Apps is a web application, the date format is depends on users’ browser and PC setting. If user input a value 6/12/2023. Do you know it is 12 June 2023 or 6 Dec 2023?
In order to enforce the format, I used the Text() function.
According to Microsoft:
By default, Text uses the language of the user running the app. The Language function returns the language tag for the current user. You can override this default value by supplying a language tag for the third argument to Text.
I added “en-GB” as the 3rd argument. Sample as below:
Text(Today(),”[$-en-GB]dd/mm/yyyy”,”en-GB”)
Then use DateValue or DateTimeValue to convert the text back to date or datetime format:
DateValue(Text(Today(),”[$-en-GB]yyyy-mm-dd”,”en-GB”));
DateTimeValue(Text(Now(),”[$-en-GB]yyyy-mm-dd hh:mm:ss”,”en-GB”));
Finally, you can insert the value into SQL with Patch function. For example:
Patch(‘WorkLog’,Defaults(‘WorkLog’),{logtime:DateValue(Text(Today(),”[$-en-GB]yyyy-mm-dd”,”en-GB”))})
Hope it helps you. I found it so inconvient to convert a date value to text and then convert again to date. Let me know if you find better approach!