No se puede reconocer la cadena como valor DateTime válido

2024-09-05T22:21:18.4966667+00:00

Buen día.

Tengo una aplicación web, desplegada en el IIS de un servidor que está en la nube.

Constantemente me sale un error de manera intermitente que indica "No se puede reconocer la cadena como valor DateTime válido" , luego de un rato la data se vuelve a consultar, y funciona bien, después nuevamente sale el error, y así consecutivamente.

Lo cual no es correcto, ya que trae mucho retraso en el ambiente productivo. Cabe recalcar que anteriormente tenían un servidor físico, y funcionaba muy bien. Ahora el error se presenta desde que pasaron al servidor en nube, sin haber hecho ningún cambio.

Ejemplo :

string fecha = "05/09/2024"; // formato dd/MM/aaaa

DateTime fechaD = Convert.ToDateTime(fecha);

Error : No se puede reconocer la cadena como valor DateTime válido

Su apoyo por favor, es urgente

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,670 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,924 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,842 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 26,706 Reputation points
    2024-09-06T02:15:43.0166667+00:00

    Hi @Palma Quintanila,Martha Jenniffer Alexandra

    This is a forum using English, please edited your post for better support.

    Regarding your issue, what is the code like?

    Sometimes the syntax is different between the physical server and cloud server.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  2. Michael Taylor 53,726 Reputation points
    2024-09-06T02:36:06.1566667+00:00

    Your code is making an assumption that isn't necessarily true. Most likely while you had your software running on a physical server then you had control over the locale and therefore the formatting of date/time values. However once you moved to the cloud that is no longer true. Although if you're running a VM in the cloud then you can control the locale information. If you're hosting your app using Azure App Services then you need to set the locale in your web.config file. This should solve your initial problem.

    I'm going to guess you periodically see failures when you see dates where the middle value is > 12 since your formatting is assuming it is month but by default it is most likely day hence 05/12 is 12 May, not 5 Dec. To properly fix this ideally you should explicitly specify the formatting to use. This eliminates any issues with OS locale. If you have a web app that needs to manage this for the user then ensure you are setting the locale information for the app instead.

    To ensure strings are parsed properly you should do something like this.

    DateTime fechaD = DateTime.ParseExact(fecha, "dd/MM/yyyy", null);
    

    When parsing strings it is preferable to use DateTime.Parse or DateTime.ParseExact. If you absolutely must use Convert.ToDateTime then note that there is no version that allows you to specify the format that is expected. Instead you'll need to either ensure the thread's CultureInfo is properly set or get the correct IFormatProvider for the locale you want to use and pass it in.

    IFormatProvider provider = new CultureInfo("en-US");
    DateTIme fechaD = Convert.ToDateTime(fecha, provider);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.