最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Azure Errors with invalid date format - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question edited Feb 5 at 20:58 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 5 at 19:18 bilporbilpor 3,8896 gold badges38 silver badges84 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The 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

发布评论

评论列表(0)

  1. 暂无评论