When I run my Azure Function app via Visual Studio 2022, my function works and the values are correct.
My function is taking data from a source where everything is stored as 'string' and I'm pushing the data to an Azure SQL Server database. My column in the database is declared as nullable DateTime
.
In my function app I have the property on my model declared as DateTime?
.
To populate the model, after ensuring I have a value I have:
claim.date_lfa_signed = Convert.ToDateTime(lfa_signed);
where lfa_signed
is a string and is in the form 'dd/mm/yyyy hh:mm:ss' so as an example the value might be 28/10/2024 22:11:00
.
Breakpointing my code it sets the property on my model and my code continues through to the end without falling into any exception blocks and the columns in the database get populated as expected.
When I deploy the function app, and run it fails with:
String '28/10/2024 22:11:00' was not recognized as a valid DateTime.
I see this in App Insights. The function is deployed as self-contained using a target framework of .NET 8.0. I have also ensured I have the environment variable in Azure FUNCTIONS_INPROC_NET8_ENABLED
is set to 1.
When I run my Azure Function app via Visual Studio 2022, my function works and the values are correct.
My function is taking data from a source where everything is stored as 'string' and I'm pushing the data to an Azure SQL Server database. My column in the database is declared as nullable DateTime
.
In my function app I have the property on my model declared as DateTime?
.
To populate the model, after ensuring I have a value I have:
claim.date_lfa_signed = Convert.ToDateTime(lfa_signed);
where lfa_signed
is a string and is in the form 'dd/mm/yyyy hh:mm:ss' so as an example the value might be 28/10/2024 22:11:00
.
Breakpointing my code it sets the property on my model and my code continues through to the end without falling into any exception blocks and the columns in the database get populated as expected.
When I deploy the function app, and run it fails with:
String '28/10/2024 22:11:00' was not recognized as a valid DateTime.
I see this in App Insights. The function is deployed as self-contained using a target framework of .NET 8.0. I have also ensured I have the environment variable in Azure FUNCTIONS_INPROC_NET8_ENABLED
is set to 1.
1 Answer
Reset to default 1The default dateformat of en-US is mm/dd/yyyy, this is why the conversion fails.
If your string-date is always formatted as dd/mm/yyyy hh:mm:ss you should use DateTime.ParseExact as shared on this StackOverflow-post:
How to Convert a Date String with Format "dd/MM/yyyy" to OS Current Culture Date Format