Convert DateTime from English to Spanish

37,387

Solution 1

Yyou can use CultureInfo to do this, if you set the current culture in the running thread the date will format in the correct culture http://msdn.microsoft.com/en-us/library/5hh873ya.aspx

in vb.net

    Dim TheDate As DateTime = DateTime.Parse("January 01 2011")
Thread.CurrentThread.CurrentCulture = New CultureInfo("es-ES")
MsgBox(TheDate.ToLongDateString)

or c#

DateTime TheDate = DateTime.Parse("January 01 2011");
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
Interaction.MsgBox(TheDate.ToLongDateString());

Solution 2

You can use the DateTime.ParseExact Method to parse the input into a DateTime value using an English CultureInfo. Then you can use the DateTime.ToString Method with a Spanish CultureInfo to convert the DateTime value to a string.

var input = "Tuesday, July 26, 2011";
var format = "dddd, MMMM dd, yyyy";

var dt = DateTime.ParseExact(input, format, new CultureInfo("en-US"));

var result = dt.ToString(format, new CultureInfo("es-ES"));
// result == "martes, julio 26, 2011"

Consider that a Spanish user might prefer the Spanish standard format over your custom format though:

var result = dt.ToString("D", new CultureInfo("es-ES"));
// result == "martes, 26 de julio de 2011"

Solution 3

Get the DateTime.Now and translate when you need.

private DateTime lastConnection = DateTime.Now;
String dateString =lastConnection.ToString("dd") +" de "+ lastConnection.ToString("MMMM",new CultureInfo("es-ES"))
Share:
37,387
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    Does anybody know how to convert a DateTime from English to Spanish?

    E.g convert:

    Monday, January 01, 2011

    into

    Lunes, Enero 01, 2011 ???

    Thanks in advance.

  • Mechanical snail
    Mechanical snail over 12 years
    +1 for noting the "dddd, MMMM dd, yyyy" format is non-standard in Spanish.
  • Exel Gamboa
    Exel Gamboa about 4 years
    For those in Puerto Rico, remember that ES is from Spain. Although we use the date in Spanish format, we use the period (.) for decimal numbers (contrary to Spain and other european countries). So in this case, use Thread.CurrentThread.CurrentCulture = new CultureInfo("es-PR"); if you are going to set the current culture in the running thread.
  • Leandro Bardelli
    Leandro Bardelli over 3 years
    @Mechanicalsnail yes is standard: [day] de [month without capital] del [year in 4 digit number]. The correct way is "del" but "de" is also accepted. (I'm spanish university teacher)