Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DateTime from English to Spanish

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.

like image 946
user864343 Avatar asked Jul 26 '11 21:07

user864343


3 Answers

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"
like image 200
dtb Avatar answered Nov 17 '22 13:11

dtb


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());
like image 23
JonAlb Avatar answered Nov 17 '22 14:11

JonAlb


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"))
like image 2
El0din Avatar answered Nov 17 '22 13:11

El0din