DateTime where do you get the format, depends on the language? [closed]

0

I am working with windows server with its English version, but on the client system it takes the date format in "M / d / yyyy".

Already change the language and date format of the server but it is not changed.

Any ideas?

    
asked by Oscar Rodriguez 26.04.2017 в 16:31
source

1 answer

1

If you mean that you want to set the text from a DateTime object without having to write a lot of your own code, maybe this is what you are looking for:

using System.Globalization;
///
DateTimeFormatInfo MxFormat = new CultureInfo("es-MX", false).DateTimeFormat;
string Fecha = DateTime.Now.ToString(MxFormat.LongDatePattern);

EDIT

According to what you mention, you want to convert a String to a DateTime . To do it respecting the format in Spanish dd/mm/aaaa you must do it like this:

DateTime dt; // A esta variable asignarás el valor.
if (DateTime.TryParseExact(MiString,             //Primero la variable
                           "dd/MM/yyyy",         //El formato esperado
                           CultureInfo.InvariantCulture, //Información de cultura
                           DateTimeStyles.None,  //Formato de análisis
                           out dt))              //Variable a la que se asignará el valor
{
    // la fecha es válida
} 
else 
{
    // la fecha no es válida
}
    
answered by 26.04.2017 / 21:47
source